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
4. (Matlal) attatimient) Consider the initial valle probleni 1<t< 2 y(1) 1 Caleulate the approximate solutions...
Question 1: Given the initial-value problem 12-21 0 <1 <1, y(0) = 1, 12+10 with exact solution v(t) = 2t +1 t2 + 1 a. Use Euler's method with h = 0.1 to approximate the solution of y b. Calculate the error bound and compare the actual error at each step to the error bound. c. Use the answers generated in part (a) and linear interpolation to approximate the following values of y, and compare them to the actual value...
Solve using Matlab Use the forward Euler method, Vi+,-Vi+(4+1-tinti ,Vi) for i= 0,1,2, , taking yo y(to) to be the initial condition, to approximate the solution at t-2 of the IVP y'=y-t2 + 1, 0-t-2, y(0) = 0.5. Use N = 2k, k = 1, 2, , 20 equispaced time steps (so to = 0 and tN-1 = 2). Make a convergence plot, computing the error by comparing with the exact solution, y: t1)2 -exp(t)/2, and plotting the error as...
Numerical Methods Consider the following IVP dy=0.01(70-y)(50-y), with y(0)-0 (a) [10 marks Use the Runge-Kutta method of order four to obtain an approximate solution to the ODE at the points t-0.5 and t1 with a step sizeh 0.5. b) [8 marks Find the exact solution analytically. (c) 7 marks] Use MATLAB to plot the graph of the true and approximate solutions in one figure over the interval [.201. Display graphically the true errors after each steps of calculations. Consider the...
///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) Plot the approximate solutions obtained using the methods of Euler, midpoint and the classic fourth order Runge Kutta with n 40 superimposed over the exact solution in the same figure. To plot multiple curves in the same figure, make use of the...
MATLAB HELP 3. Consider the equation y′ = y2 − 3x, where y(0) = 1. USE THE EULER AND RUNGE-KUTTA APPROXIMATION SCRIPTS PROVIDED IN THE PICTURES a. Use a Euler approximation with a step size of 0.25 to approximate y(2). b. Use a Runge-Kutta approximation with a step size of 0.25 to approximate y(2). c. Graph both approximation functions in the same window as a slope field for the differential equation. d. Find a formula for the actual solution (not...
Complete using MatLab 1. Consider the following initial value problem 3t2-y, y(0) = 1 Using Euler's Method and the second order Runge-Kutta method, for t E [0, 1] with a step size of h 0.05, approximate the solution to the initial value problem. Plot the true solution and the approximate solutions on the same figure. Be sure to label your axis and include an a. appropriate legend b. Verify that the analytic solution to the differential equation is given by...
MATLAB help please!!!!! 1. Use the forward Euler method Vi+,-Vi + (ti+1-tinti , yi) for i=0.1, 2, , taking yo-y(to) to be the initial condition, to approximate the solution at 2 of the IVP y'=y-t2 + 1, 0 2, y(0) = 0.5. t Use N 2k, k2,...,20 equispaced timesteps so to 0 and t-1 2) Make a convergence plot computing the error by comparing with the exact solution, y: t (t+1)2 exp(t)/2, and plotting the error as a function of...
1 st s2, y(1)1 The exact solution is given by yo) - = . 1+Int Write a MATLAB code to approximate the solution of the IVP using Midpoint (RK2) and Modified Euler methods when h [0.5 0.1 0.0s 0.01 0.005 0.001]. A) Find the vector w mid and w mod that approximates the solution of the IVP for different values of h. B) Plot the step-size h versus the relative error of both in the same figure using the LOGLOG...
462 1.231251937 4 Yy-2(3)(1.061616 237712 4. Given the initial value problem and exact solution: a) Verify the solution by the method of Undetermined Coefficients. x-y+2 y(0)4 ()3e +x+1 (10 points) b) Apply the Runge-Kutta method to approximate the solution on the interval [0.0.5] with step size [15 points h = 0.25 . Construct a table showing six-decimal-place values of the approximate solution and actual solution at each step
The Program for the code should be matlab 5. [25 pointsl Given the initial value problem with the initial conditions y(0) 2 and y'(0)10, (a) Solve analytically to obtain the exact solution y(x) (b) Solve numerically using the forward Euler, backward Euler, and fourth-order Runge Kutta methods. Please implement all three methods yourselves do not use any built- in integrators (i.e., ode45)). Integrate over 0 3 r < 4, and compare the methods with the exact solution. (For example, using...