Getting error in the Q(1) = IC, how do i apply Forward Euler to this

D = 90.5*0.4814E-3;
S = 1.814;
q0 = 1.4155;
M = 0.9953;
v0 = 1.545 /48.888;
tspan = [0 2000];
t0 = 0;
t_end = 2000;
IC = [q0,v0];
[t, Q] = euler_forward(t0,IC,t_end,1000,@dqdtfn);
q = Q(:,1);
v = Q(:,2);
% Classically E would be (1/2)Mv^2 + U
% If that applies here, then:
H = 1/2*M*v.^2 + D*(1- exp(-S*(q-q0)).^2)-(1/2);
subplot(2,1,1)
plot(t,q),grid
xlabel('t'),ylabel('q')
subplot(2,1,2)
plot(t,H),grid
xlabel('t'),ylabel('H')
function dqdt = dqdtfn(~,Q)
D = 90.5*0.4814E-3;
S = 1.814;
q0 = 1.41;
M = 0.9953;
q = Q(1);
v = Q(2);
dvdt = -2*D*S*exp(-S*(q-q0))*(1-exp(-S*(q-q0)))/M;
dqdt = [v; dvdt];
end
function [t,Q] = euler_forward(t0,IC,t_end,n,fcn)
% Solve the initial value problem
% y’ = fcn(t,y), t0 <= t <= b, y(t0)=y0
h = (t_end-t0)/n; % step size.
t = linspace(t0,t_end,n)'; % an array of n points between (and including) t0 and t_end
Q = zeros(n,1); % solution vector initialization
Q(1) = IC; % initial condition
for i = 2:n
Q(i) = Q(i-1)+h*feval(fcn,t(i-1),Q(i-1));
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by