Plotting piecewise function self-dependent
5 views (last 30 days)
Show older comments

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
0 Comments
Answers (1)
John D'Errico
on 20 May 2017
Edited: John D'Errico
on 20 May 2017
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.
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!