I need to plot a t vs. s diagram. The function is: s = (sigma - 3 + 0.072 * t * (1 + 0.072 * t)) / 0.77. I'm dying to know why the loop inside loop does not give me the correct plot. Thank you so much.
INCORRECT:
clear all;
Sigma = 0:5:30;
T = 0:1:30;
for sigma = 1:length(Sigma);
for t = 1:length(T);
s(sigma,t) = (sigma-3+0.072*t*(1+0.072*t))/0.77;
end;
end;
plot(s,T)
CORRECT:
clear all;
Sigma = 5:5:30;
T = 0:1:30;
for t = 1:length(T);
s(Sigma,t) = (Sigma-3+0.072*t*(1+0.072*t))/0.77;
end;
plot(s,T);
Thank you.*

 採用された回答

Walter Roberson
Walter Roberson 2014 年 1 月 22 日

0 投票

In your first version, you have
for sigma = 1 : length(Sigma)
which makes sigma 1, 2, 3, ... up to the number of values in Sigma.
In your second version, you use Sigma in your calculation, which uses the contents of Sigma rather than the numbers up to the length of Sigma.
Fix:
s(sigma,t) = (Sigma(sigma)-3+0.072*t*(1+0.072*t))/0.77;

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 22 日

1 投票

clear
Sigma = 5:5:30;
n=numel(Sigma)
t = linspace(0,30,n);
s=zeros(1,n);
for k = 1:n;
s(k) = (Sigma(k)-3+0.072*t(k)*(1+0.072*t(k)))/0.77;
end;
plot(t,s);

1 件のコメント

Michael Wolf
Michael Wolf 2014 年 1 月 23 日
Thank you guys so much. I figured it out

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

カテゴリ

ヘルプ センター および File ExchangeGraphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by