Step function with a ramp. Error in the creation of the ramp

1 回表示 (過去 30 日間)
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 1 月 27 日
I am trying to create a ramp which is a part of a step function in order to plot it.
x=1e-6:1e-6:L;
for i=1:10
bl(i)=(2*i-1)*9.35e-4+(i-1)*8e-5;
bu(i)=(2*i-1)*9.35e-4+i*8e-5;
I(x>=bl(i) & x<bu(i))=(i-1)*1.26e-2+150.*(x-bl(i)); % Current in part b of the unit cell
end
But I get this error:
In an assignment A(I) = B, the number of elements in B and
I must be the same.
Do you know the solution?
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 27 日
What is the initial value of I. What is the expected value of I ?
Can you explain what do you want to achieve?

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

採用された回答

Matt J
Matt J 2013 年 1 月 27 日
idx=(x>=bl(i) & x<bu(i));
I(idx)=(i-1)*1.26e-2+150.*(x(idx)-bl(i));
  7 件のコメント
Matt J
Matt J 2013 年 1 月 28 日
Here's a somewhat lazy way of doing it,
t=linspace(0,12,1e4);
f=@(x) 5.*(x<=5) +x.*(x>5 & x<=10) + 10*(x>10);
plot(t,f(t)); ylim([4,11])
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 1 月 29 日
Thank you Matt

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 1 月 27 日
編集済み: Image Analyst 2013 年 1 月 27 日
Have you considered linspace() instead?
bl = linspace(9.35e-4, 19*9.35e-4+9*8e-5, 10)
bu = linspace(9.35e-4, 19*9.35e-4+10*8e-5, 10)
output = linspace(0, 9*1.26e-2, 10)+150*(x-bl)
indexesToSet = x>=bl & x<bu % Only set those elements satisfying this.
I(indexesToSet) = output(indexesToSet)

Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 1 月 27 日
編集済み: Giorgos Papakonstantinou 2013 年 1 月 27 日
I didn't know about linspace. I would definitely try it out. Thank you both!
  1 件のコメント
Image Analyst
Image Analyst 2013 年 1 月 27 日
It's just a more MATLAB-ish way of doing things since it avoids a loop, though with only 10 iterations, looping is definitely not a bottleneck. If you had tens of millions of iterations, it might make a noticeable effect.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by