Plotting piecewise function self-dependent

조회 수: 5 (최근 30일)
Eugenio Cataldo
Eugenio Cataldo 2017년 5월 20일
댓글: Eugenio Cataldo 2017년 5월 20일
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