Help with Alogrithm????
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!