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

 採用された回答

James Tursa
James Tursa 2015 年 9 月 23 日
編集済み: James Tursa 2015 年 9 月 23 日

0 投票

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 件のコメント

qasim zia
qasim zia 2015 年 9 月 24 日
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 2015 年 9 月 24 日
編集済み: James Tursa 2015 年 9 月 24 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

質問済み:

2015 年 9 月 23 日

編集済み:

2015 年 9 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by