Question
I'm trying to code a function in MatLAB that can generate a spiderweb-like nodes on 2D coordinate system, shaped as a square with a hole
I'm trying to code a function in MatLAB that can generate a spiderweb-like nodes on 2D coordinate system, shaped as a square with a hole in it. In other words, I'm trying to mesh a square with a whole in it with quadrilateral elements. So far, my aim is to generate the nodes by entering numbers that specify how many parts I want to divide the specific parts of the shape. My function is:
function [circlecoords, sides, webnodes]= PlotWeb(q,p,m,R) ang=1.25*pi:(pi/(2*p)):1.25*pi+2*pi; hold on xp=R*cos(ang); yp=R*sin(ang);
centerx = mean(q(:,1)); centery = mean(q(:,2));
circlecoords = [(centerx + transpose(xp)) (centery + transpose(yp))];
sides(1:p+1,1)=0:(1/p):1; sides(1:p+1,2)=0; sides(p+1:2*p+1,1)=1; sides(p+1:2*p+1,2)=0:(1/p):1; sides(2*p+1:3*p+1,1)=1:-(1/p):0; sides(2*p+1:3*p+1,2)=1; sides(3*p+1:4*p+1,1)=0; sides(3*p+1:4*p+1,2)=1:-(1/p):0;
webnodes = [0 0]; for i=(1:(4*p+1)) webnodecornerdefinex = [sides(i,1) circlecoords(i,1)]; webnodecornerdefiney = [sides(i,2) circlecoords(i,2)]; arrayx = 0:((abs(circlecoords(i,1)-sides(i,1)))/m ):(abs(circlecoords(i,1)-sides(i,1))); arrayy = 0:((abs(circlecoords(i,2)-sides(i,2)))/m ):(abs(circlecoords(i,2)-sides(i,2))); webnodesupdate = [0 0];
for j = 1:m+1 if sides(i,1) ~= circlecoords(i,1) webnodesupdate(j,1)= (min(webnodecornerdefinex)) + arrayx(j); webnodefunc = polyfit(webnodecornerdefinex,webnodecornerdefiney,1); webnodesupdate(j,2)= polyval(webnodefunc, webnodesupdate(j,1)); elseif sides(i,1) == circlecoords(i,1) webnodesupdate(j,1)= (min(webnodecornerdefinex)) + arrayx(j); webnodesupdate(j,2)= (min(webnodecornerdefiney)) + arrayy(j);
end end webnodes = [webnodes; webnodesupdate]; end
scatter(sides(:,1),sides(:,2)); scatter(circlecoords(:,1),circlecoords(:,2)); scatter(webnodes(:,1),webnodes(:,2)); hold off end
I also use an additional tester code to run it:
clear all close all clc
q = [0 0; 1 0; 0 1; 1 1]; [cc, sd, wn] = PlotWeb(q,4,4,0.2)
You might notice right away that the generated shape does not distribute the nodes that has their "circle" and "sides" tips on the same x coordinate properly. My guess is that my "if" operation inside the for loop does not work properly since the algorithm tries to force the y values into the polyval although their x values are same and this will bring the error message:
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT. > In polyfit (line 79) In PlotWeb (line 34) In tester (line 6)
Also MatLAB warns me to preallocate webnodes although I believe I did.
At this point, I don't know what I overlooked.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started