How is this an exponential curve?
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Shoaibur Rahman
on 14 Jan 2015
Edited: Shoaibur Rahman
on 14 Jan 2015
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.
0 Comments
More Answers (1)
Torsten
on 14 Jan 2015
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.
0 Comments
See Also
Categories
Find more on Oceanography and Hydrology 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!