how to update value of a function at each time step
18 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Joseph Cheng
2014-8-22
编辑:Joseph Cheng
2014-8-22
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!