Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
I am puzzeled: solve the challenge

Subject: I am puzzeled: solve the challenge

From: David Edson

Date: 7 May, 2009 15:37:01

Message: 1 of 2



I have the following set of formulas to define my shape. I want to divide my shape K1 time by using the formulas in the for loop. Unfortunately, it only works for k1=5, if you change k1 valuse it fails to perform its purpose. What is wrong with matlab?



[t, b]=meshgrid((-pi/2):(pi/100):(pi/2),0:(pi/50):2*pi);
rx; ry; rz;
k1=5; k2=-2; k3=2/30;
g=(ry-2)*(abs(sin(k1.*b/2))/10 + (2.^(k2.*t)- 2.^(k3.*t))/10);

x=(rx-2).*cos(t).*cos(b);
y=(ry-2).*sin(t)+g;
z=(rz-2).*cos(t).*sin(b);

for i=1:k1+1
x(b==(2*pi/k1)*i)=0;
z(b==(2*pi/k1)*i)=0;
end
clf
mesh(x,y,z)
colormap(autumn)
grid off
axis off
axis equal
axis ij

I am wondering.........

DE

Subject: I am puzzeled: solve the challenge

From: someone

Date: 7 May, 2009 17:13:03

Message: 2 of 2

"David Edson" <tojadeb@example.com> wrote in message <gtuv6t$9bm$1@fred.mathworks.com>...
>
>
> I have the following set of formulas to define my shape. I want to divide my shape K1 time by using the formulas in the for loop. Unfortunately, it only works for k1=5, if you change k1 valuse it fails to perform its purpose. What is wrong with matlab?

I doubt there is anything wrong with MATLAB.
Its probably your code that is faulty.

>
>
>
> [t, b]=meshgrid((-pi/2):(pi/100):(pi/2),0:(pi/50):2*pi);
> rx; ry; rz;
> k1=5; k2=-2; k3=2/30;
> g=(ry-2)*(abs(sin(k1.*b/2))/10 + (2.^(k2.*t)- 2.^(k3.*t))/10);
>
> x=(rx-2).*cos(t).*cos(b);
> y=(ry-2).*sin(t)+g;
> z=(rz-2).*cos(t).*sin(b);
>
> for i=1:k1+1
> x(b==(2*pi/k1)*i)=0;
> z(b==(2*pi/k1)*i)=0;
> end
> clf
> mesh(x,y,z)
> colormap(autumn)
> grid off
> axis off
> axis equal
> axis ij
>
> I am wondering.........


For one thing, it is a very bad idea to compare two floats for equality.
Especially with a nunber like pi in it. I would be very suspicious of:
x(b==(2*pi/k1)*i)=0;
z(b==(2*pi/k1)*i)=0;
How often do you expect b to exactly equal 2*pi/k1)*i?

See Q6.1 of the MATLAB FAQ at:
http://matlabwiki.mathworks.com/MATLAB_FAQ

>
> DE

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us