how, using a for loop, can I create an array

1 回表示 (過去 30 日間)
George Amor
George Amor 2020 年 12 月 7 日
コメント済み: George Amor 2020 年 12 月 7 日
I am trying to create an array for V and A that can then be plotted.
for a = [.25:.25:4] %innder radius
b = a+2 ;%outer radius
V = (.25*pi^2).*(a+b)*(b-a).^2 %equation for Volume
A = pi^2*(b^2-a.^2) %equation for Area
end
plot(V,A) %plot Volume vs. Area
with the current code, when I run the script, V and A only have one value which is equal to using a = 4
but I want every value to be saved, not just the last value for V and A

採用された回答

per isakson
per isakson 2020 年 12 月 7 日
編集済み: per isakson 2020 年 12 月 7 日
how using a for loop
V and A are overwritten for every iteration of the loop. Try this
vec = [.25:.25:4];
len = length( vec );
A = nan( len, 1 );
V = nan( len, 1 );
for jj = 1 : len
a = vec(jj);
V(jj) = (.25*pi^2).*(a+b)*(b-a).^2 %equation for Volume
A(jj) = pi^2*(b^2-a.^2) %equation for Area
end
plot(V,A) %plot Volume vs. Area

その他の回答 (1 件)

David Hill
David Hill 2020 年 12 月 7 日
編集済み: David Hill 2020 年 12 月 7 日
No loop needed.
a=.25:.25:4;
b=a+2;
V=(.25*pi^2).*(a+b).*(b-a).^2;
A=pi^2*(b.^2-a.^2);
plot(V,A);
  1 件のコメント
George Amor
George Amor 2020 年 12 月 7 日
This is much simpler than a for loop, thank you

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

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by