Question

///MATLAB/// Consider the differential equation over the interval [0,4] with initial condition y(0)=0.

3. Consider the differential equation n y = (t3 - t2 -7t - 5)e over the interval [0,4 with initial condition y(0) = 0. (a) P

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code for Euler, Improved Euler and RK4 method clear all close all %Function for which solution have to do f-e (t,y) (tk3=h* double (f ( (t+h/2), (y+k2/2))); k4-h* double (f ( (t+h) , (y+k3))) dx(1/6) (k1+2*k2 +2 *k3+k4); t-t+h y=y+dx; t4(i+1)-xlabelt) ylabelerror) subplot (3,1,3) plot(t2,err3) title Error plot for RK4 method) xlabelt ylabelerror) 응동용을용동동을을동 을용동Error plot for Euler method 20 0 05 1 15 25 35 4 4 5 t Error plot for Midpoint method 05 15 25 3.5 t. Error plot for RK4 meth

%%Matlab code for Euler, Improved Euler and RK4 method
clear all
close all
%Function for which solution have to do
f=@(t,y) (t.^3-t.^2-7.*t-5).*exp(t);
    %Euler method
    n=40;            % number of intervals
    t=0;             % initial t
    y=0;             % initial y
    t_eval=4;        % at what point we have to evaluate
    h=(t_eval-t)/n; % step size
    t2(1)=t;
    y2(1)=y;
    for i=1:n
        %Eular Steps
        m=double(f(t,y));
        t=t+h;
        y=y+h*m;
        t2(i+1)=t;
        y2(i+1)=y;
    end
    fprintf('\n\tThe solution using Euler Method for h=%.2f at x(%.1f) is %f\n',h,t2(end),y2(end))
    %Midpoint method
    t=0;             % initial t
    y=0;             % initial x

    t3(1)=t;
    y3(1)=y;
    for i=1:n
    %midpoint steps
       m1=double(f(t,y));
       m2=double(f((t+h),(y+h*m1)));
       y=y+double(h*((m1+m2)/2));
       t=t+h;
       y3(i+1)=y;
       t3(i+1)=t;
    end
  
    fprintf('\n\tThe solution using improved Euler Method for h=%.2f at x(%.1f) is %f\n',h,t3(end),y3(end))
  
    %RK4 method
    t=0;             % initial t
    y=0;             % initial x

    t4(1)=t;
    y4(1)=y;
    for i=1:n
    %RK4 Steps
       k1=h*double(f(t,y));
       k2=h*double(f((t+h/2),(y+k1/2)));
       k3=h*double(f((t+h/2),(y+k2/2)));
       k4=h*double(f((t+h),(y+k3)));
       dx=(1/6)*(k1+2*k2+2*k3+k4);
       t=t+h;
       y=y+dx;
       t4(i+1)=t;
       y4(i+1)=y;
    end
    fprintf('\n\tThe solution using Runge Kutta 4 for h=%.2f at x(%.1f) is %f\n',h,t4(end),y4(end))

%%Exact solution
syms y(t)
eqn = diff(y,t) == (t.^3-t.^2-7.*t-5).*exp(t);
cond = y(0) == 0;
ySol(t) = dsolve(eqn,cond);

y_ext=ySol(t4);
fprintf('Exact solution is y(t)=')
disp(ySol)


%%Plotting solution using Euler method
figure(1)
plot(t2,y2,'r',t3,y3,'b',t4,y4,'g',t4,y_ext,'m')

fprintf('\n\n\tError norm using Euler method is %f.\n',norm(y_ext-y2))
fprintf('\tError norm using Modified Euler method is %f.\n',norm(y_ext-y3))
fprintf('\tError norm using Runge Kutta method is %f.\n',norm(y_ext-y4))

xlabel('t')
ylabel('y(t)')
title('Solution plot y(t) vs. t')
legend('Euler Method','Midpoint','RK4 Method','Exact solution','Location','northeast')
grid on

err1=abs(y_ext-y2);
err2=abs(y_ext-y3);
err3=abs(y_ext-y4);

figure(2)
subplot(3,1,1)
plot(t2,err1)
title('Error plot for Euler method')
xlabel('t')
ylabel('error')

subplot(3,1,2)
plot(t2,err2)
title('Error plot for Midpoint method')
xlabel('t')
ylabel('error')

subplot(3,1,3)
plot(t2,err3)
title('Error plot for RK4 method')
xlabel('t')
ylabel('error')

%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
///MATLAB/// Consider the differential equation over the interval [0,4] with initial condition y(0)=0. 3. Consider the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT