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

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

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 I have followed the same procedure to create a plot based on the integral of the equation that I wrote above. The problem is that I have discontinuities in the limits of the intervals when I plot the integral of the equation. I haven't been able to fix until now. I am attaching you the code below. Perhaps you can see something that I cannot see.
clc
clear
jmpp=350; % [A/m^2]
Rb=0.01; % [Ω/sq]
L=0.0195;
Wf=1000e-6;
lf=L*0.481;
n=1;
D=L./(2*n);
S=D-Wf./2;
Wb=L-2.*lf;
x=1e-6:1e-6:L;
Iline=(4*jmpp*S*lf)/Wf; % line current
I=zeros;
P=zeros;
Pb=zeros;
Pi=zeros;
[al, au, bl, bu, cl, cu]=deal(zeros(1,n));
for i=1:n
al(i)=2*(i-1)*S+(i-1)*Wf;
au(i)=(2*i-1)*S+(i-1)*Wf;
bl(i)=(2*i-1)*S+(i-1)*Wf;
bu(i)=(2*i-1)*S+i*Wf;
cl(i)=(2*i-1)*S+i*Wf;
cu(i)=2*i*S+i*Wf;
a=(x>=al(i) & x<au(i));
b=(x>=bl(i) & x<bu(i));
c=(x>=cl(i) & x<cu(i));
t=(x>=al(i) & x<=cu(i));
I(a)=(i-1)*Iline*Wf;
P(a)=(Iline.^2.*Rb.*Wf.^2.*(x(a)-al(i)).*(i - 1).^2)./Wb;
Psuma(i)=(Iline^2*Rb*S*Wf^2*(i - 1)^2)/Wb;
I(b)=(i-1)*Iline*Wf+Iline.*(x(b)-bl(i));
P(b)=(Iline.^2.*Rb.*(x(b)-bl(i)).*(3.*Wf.^2 - 6.*Wf.^2.*i - 3.*Wf.*(x(b)-bl(i)) + (x(b)-bl(i)).^2 + 3.*Wf.^2.*i.^2 + 3.*Wf.*i.*(x(b)-bl(i))))./(3.*Wb)+Psuma(i);
Psumb(i)=(Iline^2*Rb*Wf*(12*S^2*i^2 - 12*S^2*i + 3*S^2 + 24*S*Wf*i^2 - 30*S*Wf*i + 9*S*Wf + 12*Wf^2*i^2 - 18*Wf^2*i + 7*Wf^2))/(3*Wb);
I(c)=i*Iline*Wf;
P(c)=(Iline.^2.*Rb.*Wf.^2.*i.^2.*(x(c)-cl(i)))./Wb+Psumb(i);
Psumc(i)=(Iline^2*Rb*S*Wf^2*i^2)/Wb;
I(x>=cu(i))=i*Iline*Wf;
P(x>=cu(i))=Psumc(i);
end
figure(1)
plot(x*1e2,I)
figure(2)
plot(x*1e2,P)
Thank you once again!
Matt J
Matt J 2013년 1월 28일
편집: Matt J 2013년 1월 28일
I don't know if its worth tracing the error. If the signal being integrated has been created successfully, just use CUMTRAPZ, or similar, to get its integral.
Also, now that the loop is growing complicated and unwieldy, using LINSPACE like ImageAnalyst suggested, and other vectorized methods is probably a good path to making the code simpler and more error-proof.
Yes you are right but it seems really a challenging task for someone with little or no experience in matlab. And indeed because the loop seems complicated that's why I run it for one only iteration.
I know that the integration is correct because I have done it with symbolic toolbox and I have assigned values which to crosscheck the result. So I think it worth a try to find the error. Thank you
Matt J
Matt J 2013년 1월 28일
편집: Matt J 2013년 1월 28일
Yes you are right but it seems really a challenging task for someone with little or no experience in matlab.
I doubt it's as complicated as chasing bugs in a complicated for-loop.
I know that the integration is correct because I have done it with symbolic toolbox
I think you misunderstood my point. I'm happy to believe that your symbolic version of the integral calculation was correct. That's still no reason to try to re-invent numerical integration in a for-loop when MATLAB already gives you numerical integration commands like CUMTRAPZ which can do it in one line.
After spending some time I have noticed that there is a discontinuity also in the first equation. So in order to make the problem more clear. Suppose a function with two different equations.
f1(x) a<=x<=b
f2(x) b<=x<=c
and the function is continuous at x=b, such as
f1(b)=f2(b).
The problem that I have actually is not about loops or anything else. It is about setting up the right boundaries in order to achieve continuity in matlab. So my question is how to do that? And to be more specific my problem is with three functions (first constant, second linear, third constant:
ie:
f1(x)=5 in [0,5]
f2(x)=x in [5,10]
f3(x)=10 in [10,12]
How am I going to set the boundaries in matlab in order to achieve continuity in the boundaries.?
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])

댓글을 달려면 로그인하십시오.

추가 답변 (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일

0 개 추천

I didn't know about linspace. I would definitely try it out. Thank you both!

댓글 수: 1

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by