how to update value of a function at each time step
14 views (last 30 days)
Show older comments
Hi everyone, I am really stuck in creating a code that updates the value of my function K(t) at each time step and plots it. Seems quite simple but cant get my head around it.
at t=0 K(old)=3
at t=1 K(new_1)=K(old)+dt*dK/dt
at t=2 K(new_2)=K(new_1)+ dt*dK/dt
where dK/dt=4*exp(-30*(t - 3).^2)
for each subsequent time step we update the previous value with dK/dt
I would like to plot K(new) for t=0:0.01:10
Thanks!!
0 Comments
Accepted Answer
Joseph Cheng
on 22 Aug 2014
Edited: Joseph Cheng
on 22 Aug 2014
So we start off with something like this
dt = 0.01;
t=0:dt:10;
K = zeros(size(t)); %initialize it
K(1) = 3;%setup initial value
for ind=2:length(t)
K(ind) = K(ind-1)+dt*dK/dt; where t in your dKdt is subbed by t(ind);
end
figure,plot(t,K);
So what is performed above is that in the for loop i start at index of 2 to however long your t variable is. such that the t needed for your t=2 is the t(2) built by the t=0:dt:10; then as you can see from the equation i am looking at the current index and that is equal to the previous value + the defined 0.01 value for dt and the dK/dt.
More Answers (0)
See Also
Categories
Find more on Annotations 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!