How is this an exponential curve?

조회 수: 1 (최근 30일)
cgo
cgo 2015년 1월 14일
편집: Shoaibur Rahman 2015년 1월 14일
Hello,
I would like your help on the following code.
How does this trace an exponential curve? N=8000; % number of steps to take T=8; % maximum time h=T/N; % time step t=(0:h:T); % t is the vector [0 1h 2h 3h ... Nh] y=zeros(size(t)); % prepare place to store locations
y(1)=3; % initial height
for i=1:N % start taking steps
y(i+1)=y(i)-y(i)*h;
end;
plot(t,y), hold on % plot more permanently
y(1)=-2; % initial height
for i=1:N % start taking steps
y(i+1)=y(i)-y(i)*h;
end;
plot(t,y); % plot more permanently
Please help.
Thanks

채택된 답변

Shoaibur Rahman
Shoaibur Rahman 2015년 1월 14일
편집: Shoaibur Rahman 2015년 1월 14일
Convert your difference equation into differential equation:
y(i+1) = y(i)-y(i)*h;
y(i+1) - y(i) = -y(i)*h;
(y(i+1) - y(i))/h = -y(i);
dy/dt = -y
Time step h is replaced by dt to make the equation more understandable. Now, solve this differential equation using variable separation method:
dy/y = -dt
ln|y| = -t + C
y = exp(-t+C)
y = A*exp(-t)
At t = 0, A = y(0) = 3, in your case, y(0) is equivalent to y(1) in Matlab
y = 3*exp(-t)
This means that y is exponential. However, when solving using difference equation, make sure that h or the time is small enough to keep the solution stable. For this particular equation, h<1 . Use a larger value of h in your code just to see the effect.

추가 답변 (1개)

Torsten
Torsten 2015년 1월 14일
The above code is an implementation of the explicit Euler method to solve the differential equations
y'=-y, y(0)=3
y'=-y, y(0)=-2
The above differential equations have solutions
y(t)=3*exp(-t)
y(t)=-2*exp(-t)
So yes, the program generates approximations to exponentially decaying curves.
Best wishes
Torsten.

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by