Consider the ODE dudt cos2pit u0 1 Write a forloop in MatL

Consider the ODE:

du/dt = cos(2*pi*t), u(0) = 1

Write a for-loop in MatLab to solve this equation using the forward Euler Method over the time interval 0<t<10?

--------------------------------------------------------------------------------------------

Here\'s what I have tried so far:

% Define Variables
tmin = 0;
tmax = 10;
nt = 100;
t = linspace(tmin,tmax,nt);
u0 = 1.0;

% Create a vector called uexact to solve for exact solution
uexact = (sin(2*pi*t)/(2*pi))+1;

% % Forward Euler
u(1) = u0
for n = 1:nt
% u(n+1) =u(n)+1*cos(2*pi*t(n))
% dudt = cos(2*pi*t(n))
% u(n+1) = dudt*(t(n)-t(n-1))+u(n)
% u(n) = cos(2*pi*(t(n)-t(n-1)))*u(n-1)+ufe(n-1);
end

Solution

% Initialisation of variables tinit = 0; tfinal = 10; uinit = 1; n = 100; % Calculation of h from tinit, tfinal, and n h=(tfinal-tinit)/n; % Initialization of t and u as column vectors t=[tinit zeros(1,n)]; u=[uinit zeros(1,n)]; % Calculation of t and u for i=1:n t(i+1)=t(i)+h; u(i+1)=u(i)+h*cos(2*pi*t(i)); end end
Consider the ODE: du/dt = cos(2*pi*t), u(0) = 1 Write a for-loop in MatLab to solve this equation using the forward Euler Method over the time interval 0<t&l

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site