Question: Consider the following ODE: y ` = - 2 xsin ( x ^ 2 ) y Solve the ODE using the fourth order Runge -
Consider the following ODE:
yxsinxy
Solve the ODE using the fourth order RungeKutta RK method:
k Fxiyi
k Fxidelta x yikdelta x
k Fxidelta x yikdelta x
k Fxidelta x yikdelta x
yi yi delta xkkk k
where Fxiyi yxi yixisinxiyi
Interval: x
Number of points: nx
Initial condition: yxe
Hint: eis the mathematical constant ewhich is available in Maltab through the exp function. To get the value of eyou can call the expfunction with input : e exp
discretize x
define start value of x: xs
define end value of x: xe
define number of points: nx
discretize x
compute the grid spacing dx
exact solution compute on finer discretization for visualization
xvis linspacexsxe;
yvis expcosxvis.;
preallocate array to store solution y with nx rows and 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 to xcheck
assign the value to ycheck
call function dydx and pass in xcheck and ycheck, store the returned
value in dydxcheck
solve the ode using RK
apply initial condition
march forward and compute the solution using the RK method
plot results
plot y over x as a solid blue line with line width of
plot yvis over xvis as a dashed red line with a line width of in the same plot
customize the plot as follows:
switch on grid
swith font size to
set the xaxis label to x
set the yaxis label to y
add the legend entries rk and 'exact'
MATLAB Code Please
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
