Help with Alogrithm????

im trying to do linear interpolation the parametric equations for the ellipse i have are
x=30*cos(t)
y=15*sin(t)
when i simply put it and plot it the graph is fine but when i try something like this
s=pi/180; %to make radians.
>> for i=1:360; %for every degree
t=s*i; %degree in to radians
x=30*cos(t);
y=15*sin(t);
end
>> plot(x,y)
please help Im new with this coding

 Accepted Answer

James Tursa
James Tursa on 23 Sep 2015
Edited: James Tursa on 23 Sep 2015
You could save the x and y result from each iteration in a vector, but rather that do that you don't really need the for loop at all. E.g.,
s=pi/180; %to make radians.
i=1:360; %for every degree
t=s*i; %degree in to radians
x=30*cos(t);
y=15*sin(t);
plot(x,y)

2 Comments

qasim zia
qasim zia on 24 Sep 2015
That actually works but can u please tell me why didnt it print out the graph when i tried a for loop? (was it because it was unnecessary?)
James Tursa
James Tursa on 24 Sep 2015
Edited: James Tursa on 24 Sep 2015
It didn't print out what you expected because you were not saving the results of each iteration in a vector. The way you have it coded, each iteration the previous values of x and y get overwritten by new values of x and y. At the end of the loop, you only have the last values of x and y, not all of the values of x and y. Here is how you could fix that if you wanted to use the loop (making x and y vectors):
s=pi/180; %to make radians.
for i=1:360; %for every degree
t=s*i; %degree in to radians
x(i)=30*cos(t); % save in the i'th element
y(i)=15*sin(t); % save in the i'th element
end
plot(x,y)
But note that the simple changing of x and y to x(i) and y(i) only works well because the indexing i=1:360 started at 1 and counted by 1. Also, we didn't pre-allocate x or y (not really needed for this small problem). But if the indexing changed or the problem got bigger, this simple "fix" I have shown would need to be modified.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 23 Sep 2015

Edited:

on 24 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!