Question

4. (Matlal) attatimient) Consider the initial valle probleni 1<t< 2 y(1) 1 Caleulate the approximate solutions using forward Euler method, two stage and four stage Runge Kutta method with h 1/10, 1/20,1/40 and compute the maximum errors between the exact solution and the approximate solutions. Use this maximum error to verify the convergence order of each method (1, 2, and 1). Note: the exact solution is

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

Answer: Code: Define method function [ t, y 1 - euler f, tRange, yInitial, h ) %Find value nums teps = ( tRange (2) -tRange (

%End end %Define function function [t, y TwostageRK ( f, tRange, yInitial, h) %Find value numSteps = ( tRange (2) -tRange ( 1

bf (t (kk) +0.5*h, y(kk) +0.5*a); %Compute t (kk+1)t (kk) hi %compute y (kkt1)- y(kk) bi %End end %End end %Define function f

y (1)yInitial; %Loop for kk-1: numSteps &Compute &Compute b f(t (kk) +0.5*h, y(kk) +0.5*a); = %Compute d = f(t(kk) +h, y(kk)

end %Define function defun (t,y) -1*y*y*Y: %define function f r exact s luti n soln (t) sqrt (1/ (2*t +1)) %Define h value hv

[t,y] = euler (defun , [1,2], 1, h); %Create realVal -zeros (length (t), 1) %Loop kk-1: length (t) %Find value realVal (kk) -

realVal2zeros (length (t2), 1); %Create vect r maxErr-zeros (length (t2), 1); %Loop for kk= 1: length (t2) %Find value realva

t3,y3 -FourStageRK (defun, [1,2], 1, h) %Create vector realVal3 -zeros (length (t3),1) %create vector max_Err- zeros (length

end Sample Output Η value : Θ . 100000 Forward Euler Method: Maximum Error: 0.422650 Two Stage Runge Kutta Method Maximum Err

Copyable code:

%Define method
function [ t, y ] = euler ( f, tRange, yInitial, h )

   %Find value
   numSteps = ( tRange(2) - tRange(1) ) / h;

   %Create vector
   t=zeros(numSteps+1,1);

   %Set value
   t(1) = tRange(1);

   %Set value
   y(1) = yInitial;

   %Loop
   for kk = 1 : numSteps

       %Find value
       t(kk+1) = t(kk) + h;

       %Find value
       y(kk+1,:) = y(kk,:) + h * f(t(kk), y(kk));

   %End
   end

%End
end

%Define function
function [ t, y ] = TwoStageRK ( f, tRange, yInitial, h )

   %Find value
   numSteps = ( tRange(2) - tRange(1) ) / h;

   %Create vector
   t=zeros(numSteps+1,1);

   %Set value
   t(1) = tRange(1);

   %Set value
   y(1) = yInitial;

   %Loop
   for kk = 1 : numSteps

       %Find value
       a = h*f(t(kk), y(kk));

       %Find value
       b = f(t(kk)+0.5*h, y(kk)+0.5*a);

       %Compute
       t(kk+1) = t(kk) + h;

       %compute
       y(kk+1) = y(kk) + b;

   %End
   end

%End
end

%Define function
function [ t, y ] = FourStageRK ( f, tRange, yInitial, h )

   %Find value
   numSteps = ( tRange(2) - tRange(1) ) / h;

   %Create vector
   t=zeros(numSteps+1,1);

   %Set value
   t(1) = tRange(1);

   %Set value
   y(1) = yInitial;

   %Loop
   for kk = 1 : numSteps

       %Compute
       a = f(t(kk), y(kk));

       %Compute
       b = f(t(kk)+0.5*h, y(kk)+0.5*a);

       %Compute
       c = f(t(kk)+0.5*h, y(kk)+0.5*h*b);

       %Compute
       d = f(t(kk) +h, y(kk)+c*h);

       %Find value
       t(kk+1) = t(kk) + h;

       %Find value
       y(kk+1) = y(kk) + (1/6) *(a+2*b+2*c + d)*h;

   %End
   end

%End
end

%Define function
defun = @(t,y) -1*y*y*y;

%define function for exact solution
soln = @(t) sqrt(1/(2*t +1));

%Define h value
hval = [0.1 0.05 0.025];

%Loop
for kk=1: length(hval)

   %Get current h value
   h = hval(kk);

   %Print
   fprintf("\n H value: %f", h);

   %Print
   fprintf("\n-----------------------\n");

   %Print
   fprintf("\n Forward Euler Method:");

   %Call function
   [t,y] = euler(defun, [1,2], 1, h);

   %Create
   realVal = zeros(length(t), 1);

   %Loop
   for kk=1: length(t)

       %Find value
       realVal (kk) = soln(t(kk));

   %End
   end

   %Find value
   maxErr1 = max(abs(realVal-y));

   %Print
   fprintf("\n Maximum Error: %f\n", maxErr1);

   %Print
   fprintf("\n Two Stage Runge Kutta Method:");

   %Call function
   [t2,y2] = TwoStageRK(defun, [1,2], 1, h);

   %Create vector
   realVal2 = zeros(length(t2), 1);

   %Create vector
   maxErr = zeros(length(t2), 1);

   %Loop
   for kk=1: length(t2)

       %Find value
       realVal2 (kk) = soln(t(kk));

       %Compute error
       maxErr(kk) = abs(realVal2(kk) - y2(kk));

   %End
   end

   %Find max error
   maxErr2 = max(maxErr);

   %Print
   fprintf("\n Maximum Error: %f\n", maxErr2);

   %Define function
   fprintf("\n Four Stage Runge Kutta Method:");

   %Call function
   [t3,y3] = FourStageRK(defun, [1,2], 1, h);

   %Create vector
   realVal3 = zeros(length(t3), 1);

   %Create vector
   max_Err = zeros(length(t3), 1);

   %Loop
   for kk=1: length(t2)

       %Find value
       realVal3 (kk) = soln(t(kk));

       %Find error
       max_Err(kk) = abs(realVal3(kk) - y3(kk));

   %End
   end

   %Find max error
   maxErr3 = max(max_Err);

   %Print
   fprintf("\n Maximum Error: %f\n", maxErr3);

%End
end

Add a comment
Know the answer?
Add Answer to:
4. (Matlal) attatimient) Consider the initial valle probleni 1<t< 2 y(1) 1 Caleulate the approximate solutions...
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