In: Advanced Math
Homework 1.1. (a) Find the solution of the initial value problem x' = x^(3/8) , x(0)=1 , for all t, where x = x(t). (b) Find the numerical solution on the interval 0 ≤ t ≤ 1 in steps of h = 0.05 and compare its graph with that of the exact solution. You can do this in Excel and turn in a printout of the spreadsheet and graphs.
%Matlab code for numerical solution of ode using RK4
method
clear all
close all
%function for which Solution have to do
fun=@(t,x) x.^(3/8);
%exact solution
y_ext=@(t) (5/8.*t+1).^(8/5);
fprintf('Exact solution for given initial condition is
x(t)=')
disp(y_ext)
%initial guess
tinit=0; xinit=1;
tend=1;
[t_rk4,x_rk4]=RK4(fun,tinit,xinit,tend,0.05);
x_exact=double(y_ext(t_rk4));
error=norm(x_exact-x_rk4);
fprintf('Error norm in Classic RK4 is
%e\n',error)
fprintf('\tFor n=%d value of x(%2.2f) is
%f\n',2000,t_rk4(end),x_rk4(end))
plot(t_rk4,x_exact,'linewidth',2)
hold on
plot(t_rk4,x_rk4,'--')
ylabel('t')
xlabel('x(t)')
title('t vs. x(t) plot')
legend('Actual solution','RK4 Solution','location','best')
%%Matlab function for Runge Kutta Method
function [t_rk,y_rk]=RK4(f,tinit,yinit,tend,h)
% RK4 method
% h amount of intervals
t=tinit; % initial
t
y=yinit; % initial
y
t_eval=tend; % at what
point we have to evaluate
n=(t_eval-t)/h; % Number of steps
t_rk(1)=t;
y_rk(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)));
dy=(1/6)*(k1+2*k2+2*k3+k4);
t=t+h;
y=y+dy;
t_rk(i+1)=t;
y_rk(i+1)=y;
end
end
%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%