Question: Consider the following ODE: y ` = - 2 xsin ( x ^ 2 ) y Solve the ODE using the fourth order Runge -

Consider the following ODE:
y`=-2xsin(x^2)y
Solve the ODE using the fourth order Runge-Kutta (RK4) method:
k1= F(x_i,y_i)
k2= F(xi+(1/2)(delta x), y_i+(1/2)(k1)(delta x)
k3= F(xi+(1/2)(delta x), y_i+(1/2)(k2)(delta x)
k4= F(xi+(delta x), yi+k3(delta x)
y_(i+1)= y_i +(1/6)(delta x)(k1+2k2+2k3+ k4)
where F(x_i,y_i)= y`(x_i, y_i)=-x_i(sin[(x_i)^2](y_i)
Interval: 0<= x <=5
Number of points: n_x =100
Initial condition: y(x=0)=e
Hint: eis the mathematical constant ewhich is available in Maltab through the exp() function. To get the value of eyou can call the exp()function with input 1: e = exp(1)
discretize x
define start value of x: x_s
define end value of x: x_e
define number of points: n_x
discretize x
compute the grid spacing dx
exact solution (compute on finer discretization for visualization)
x_vis = linspace(x_s,x_e,1000);
y_vis = exp(cos(x_vis.^2));
preallocate array to store solution y with n_x rows and 1 column
define ode function to update solution
define an anonymous function (function handle) with the following properties
name: dydx
inputs: x and y (both scalars)
ouptut: the rate of change of y (scalar)
check dydx function
assign the value 2 to x_check
assign the value 1 to y_check
call function dydx and pass in x_check and y_check, store the returned
value in dydx_check
solve the ode using RK4
apply initial condition
march forward and compute the solution using the RK4 method
plot results
plot y over x as a solid blue line with line width of 4
plot y_vis over x_vis as a dashed red line with a line width of 4 in the same plot
customize the plot as follows:
- switch on grid
- swith font size to 20
- set the x-axis label to 'x'
- set the y-axis label to 'y'
- add the legend entries 'rk4' and 'exact'
MATLAB Code Please

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!