Getting syntax error when using for loop.

As title says; I am getting a syntax error when using the following code. The error points towards the d on "end"
F0=3.18;
m=0.0112;
k=31.2;
r1=0.22;
w0=sqrt(k/m);
w=linspace(0,400,400);
r=linespace(0.11,1.1,10);
for i=1:10
x0(:,i)=(F0/m)./(sqrt(((w0^2)-(w.^2))+((j*w*r(i)/m))));
end

5 件のコメント

Steven Lord
Steven Lord 2019 年 4 月 23 日
Copying and pasting this code into MATLAB (after correcting the "linespace" typo, which I assume should be linspace) I did not receive an error.
Can you show us the full text of the error message (all the text displayed in red) you receive when you run this code?
Jack Upton
Jack Upton 2019 年 4 月 23 日
It was a syntax error regarding the linspace and there was also a bracket error which I have now edited in my question.
Jack Upton
Jack Upton 2019 年 4 月 23 日
How would I now allocate a different colour for each potted data line?
Walter Roberson
Walter Roberson 2019 年 4 月 23 日
h = plot(w, x0);
will automatically use a different color or line pattern for each of the 10 lines. There are 7 default colors, so the last 3 would end up re-using the colors of the first 3, but with a different line style.
You can assign specific colors to the lines by using,
set(h(1), 'Color', [.3 .9 .2]);
set(h(2), 'Color', 'g');
set(h(3), 'Color', [1, 1/3, 7/10]);
and so on. If you have an array of color values, you can create a loop. For example,
NL = length(h);
cmap = copper( NL );
for K = 1 : NL
set(h(K), 'Color', cmap(K, :))
end
Jack Upton
Jack Upton 2019 年 4 月 23 日
Incredible, thank you!

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

 採用された回答

madhan ravi
madhan ravi 2019 年 4 月 23 日

0 投票

r = linspace... % also consider preallocating x0

2 件のコメント

Jack Upton
Jack Upton 2019 年 4 月 23 日
What do you mean pre-allocating x0,
sorry Im rather new to this.
Walter Roberson
Walter Roberson 2019 年 4 月 23 日
Before the loop,
x0 = zeros(length(w), length(r));

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

その他の回答 (0 件)

カテゴリ

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

製品

質問済み:

2019 年 4 月 23 日

コメント済み:

2019 年 4 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by