Question
choosing C to make P(t) match P(0) at t = 0. Compute the maximum error
over 0 ≤ t ≤ 1 for each solution you obtain. How do the errors change with
h for the two methods?

dP aP (PM-P). With a 1 and PM 10, solve this equation with both Eulers method and Heuns method for step sizes h = 10-k for
0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code for Euler and Heun method clear all close all all initial conditions t in-0 t end-1: P_in-l; a-1; Pm-10; Functiont_in-t (1); t max-t_ end; n« ( t-max-t-in )/h; %Initial t Final t of %number steps Runge Kutta 4 iterations for i-1: n Pl (i+using Heun method at t-1.000000 value of P( 1.000000) is 9.995863. Exact solution at t-1.000000 is 9.995916 Error in Euler me

%Matlab code for Euler and Heun method
clear all
close all

%all initial conditions
t_in=0; t_end=1;
P_in=1;
a=1; Pm=10;
%Function for exact solution

C=(1/(Pm-1));
P_ext=@(t) (Pm*C*exp(a*Pm*t))/(1+C*exp(Pm*a*t));
fprintf('Displaying exact solution\n')
disp(P_ext)

fprintf('Here value of C=%f\n\n',C)

%loop for step size
for k=1:3
    h=10^-k;
    [P_euler,t_elr]=euler(h,P_in,t_in,t_end);
    [P_heun,t_hnn]=heun(h,P_in,t_in,t_end);
  
    fprintf('For step size h=%f\n',h)
    fprintf('\tUsing Euler method at t=%f value of P(%f) is %f.\n',t_elr(end),t_elr(end),P_euler(end))
  
    fprintf('\tUsing Heun method at t=%f value of P(%f) is %f.\n',t_hnn(end),t_hnn(end),P_heun(end))
  
    fprintf('\tExact solution at t=%f is %f.\n',t_end,P_ext(t_end))
  
    fprintf('\tError in Euler method is %e.\n',abs(P_ext(t_end)-P_euler(end)))
    fprintf('\tError in Heun method is %e.\n\n',abs(P_ext(t_end)-P_heun(end)))
end
  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%function for euler method

function [P1,t]=euler(h,P_in,t_in,t_end)
  
    %all parameter values
    a=1; Pm=10;
    %function (i)
    f=@(t,P) a*P*(Pm-P);
  
    %initial conditions
    t(1)=t_in;P1(1)=P_in;
              
    t_in=t(1);            %Initial t
    t_max=t_end;          %Final t
    n=(t_max-t_in)/h; %number of steps
    %Runge Kutta 4 iterations
    for i=1:n
        t(i+1)=t_in+i*h;
        P1(i+1)=double(P1(i)+h*(f(t(i),P1(i))));

    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%function for heun method

function [P1,t]=heun(h,P_in,t_in,t_end)

    %all parameter values
    a=1; Pm=10;
    %function (i)
    f=@(t,P) a*P*(Pm-P);
    %initial conditions
    t(1)=t_in;P1(1)=P_in;
              
    t_max=t_end;          %Final t
    tn=t_in:h:t_max;
    %Runge Kutta 1 iterations
    for i=1:length(tn)-1
      
        t(i+1)= t(i)+h;
        m1=double(f(t,P1(i)));
        m2=double(f((t+h),(P1(i)+h*m1)));
        P1(i+1)=P1(i)+double(h*((m1+m2)/2));
      
     
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


alcula+

Add a comment
Know the answer?
Add Answer to:
choosing C to make P(t) match P(0) at t = 0. Compute the maximum error over 0 ≤ t ≤ 1 for each solution you obtain. How do the errors change with h for the two methods? dP aP (PM-P). With a 1 and...
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