How is this an exponential curve?

1 次查看(过去 30 天)
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 中查找有关 Oceanography and Hydrology 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by