Plotting piecewise function self-dependent

5 次查看(过去 30 天)
Hello,
I'm new to MATLAB and I have to plot a piecewise function and I can't manage to get it. The function is the (20) in the image, is a piecewise function which initial condition depends on the function itselft in the previous interval. It comes from the solution of the differential equation (18) with condition (19). The parameter m is equal to 0.02 and T, the "period of pulse" (from t_(n) to t_(n+1)) is 2. The "S^+" in the first of (20) stands for the value of the function at a time immediately before t_(n), when the second of (20) is applied.
I tried to write the intervals manually but there is some concept error, it already stucks at line 2 saying: "Subscript indices must either be real positive integers or logicals.
Error in Vaccini (line 2) y(0.1)=0.055;"
my code is (the first two rows are not displayed in the window, I don't know why...):
t=0:.1:8; y(0)=0.055;
for i=1:lenght(t)
if(0<=t(i))&(t(i)<=2)
y(t)=1+(y(0.1)-1)*exp(-0.02*(t));
elseif(2<=t(i))&(t(i)<=4)
y(t)=1+((1-0.05)*(y(2)-1))*exp(-0.02*(t-2));
elseif(4<=t(i))&(t(i)<=6)
y(t)=1+((1-0.05)*(y(4)-1))*exp(-0.02*(t-4));
elseif(6<=t(i))&(t(i)<=8)
y(t)=1+((1-0.05)*(y(6)-1))*exp(-0.02*(t-6));
end
end
plot(t,y);
If you have a better implementation it's welcome, with my knowledge I couldn't find anything better...
Thank you in advance

回答(1 个)

John D'Errico
John D'Errico 2017-5-20
编辑:John D'Errico 2017-5-20
Matlab does NOT use zero based indexing. So this is an illegal expression:
y(0)=0.055;
Read the error message again. It told you exactly that.
Expressions like this will also cause failure:
y(t)=1+(y(0.1)-1)*exp(-0.02*(t));
Here you are trying to index what is apparently a vector y, with the floating point number t, which takes on non-integer values. You also try to access y(0.1). Where does the 0.1 element of a vector lie in memory? I know, it should lie somewhere between the zero'th and the first element, but that would seem to be a real problem.
Again, a vector or matrix index MUST be an integer that is greater than zero. You are creating and using y as if it is a vector.
  1 个评论
Eugenio Cataldo
Eugenio Cataldo 2017-5-20
Ok, I tried to correct that problem and I managed to plot a function. What I don't understand is the first "pulse", the one at x=2 (the abscissa, the horizontal axis). It does not perform a reduction by 1/2 like the others, but the code is the same as for x=4 and x=6! Here is the new code:
t=0:.1:8;
for i=1:length(t)
if(0<=t(i))&(t(i)<=2)
y(i)=1+(0.0556-1)*exp(-0.02*(t(i)));
elseif(2<=t(i))&(t(i)<=4)
y(i)=1+((1-0.5)*y(2)-1)*exp(-0.02*(t(i)-2));
elseif(4<=t(i))&(t(i)<=6)
y(i)=1+((1-0.5)*y(4)-1)*exp(-0.02*(t(i)-4));
elseif(6<=t(i))&(t(i)<=8)
y(i)=1+((1-0.5)*y(6)-1)*exp(-0.02*(t(i)-6));
end
end
plot(t,y);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Geometric Transformation and Image Registration 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by