Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help with part of this matlab problem. The finite difference method is incorrect. I have attached the promt, my code, and the errors
I need help with part of this matlab problem. The finite difference method is incorrect. I have attached the promt, my code, and the errors Im encountering.
Consider the following ODE:
for with and
Write a script that:
named ys
finite difference approximations in your formulation.
b Solve for using the finite difference method with Save result as a column vector named
c Solve for using the finite difference method with Save result as a column vector named
d Solve for using the finite difference method with Save result as a column vector named
e Compare the accuracy of your results for bc and d by doing the following:
vectors of values. Note this means you will need to extract only the relevant values corresponding to the specified nodes from your solutions for bc and d to use in this calculation.
Code:
Initial conditions
y;
zguess; First guess for the slope at x
zguess; Second guess for the slope at x
Define the system of ODEs within the script
odesys @x yzyz; cosx xyz yz;
Specify the x values where the solution is to be evaluated
xeval linspace; points from to
First shot
x yz odeodesys xeval, y zguess;
Second shot
x yz odeodesys xeval, y zguess;
Interpolate to find a better guess for z
zfinal interpyzend yzendzguess zguess 'linear', 'extrap';
Final shot with interpolated initial slope
xfinal, yzfinal odeodesys xeval, y zfinal;
Extract the final solution for y
yS yzfinal:; elements
Part : Finite Difference Method
Define the ODE parameters
xend ;
y;
yend ;
Define the range of dx values
dxvalues ;
Loop over the dx values
for i :lengthdxvalues
dx dxvaluesi;
x :dx:xend;
N lengthx;
Initialize the matrix A for the linear system Ax b
A diagonesN diagonesN diagonesN;
A A dx;
Initialize the vector b for the linear system Ax b
b cosx dx;
Apply boundary conditions
A :) ; A; b y;
Aend :) ; Aend end; bend yend;
Solve the linear system
y Ab;
Store the solution in the specified variable
if dx
ydx y;
elseif dx
ydx y;
elseif dx
ydx y;
end
end
Define the nodes for comparison
nodes ::;
Extract the relevant y values
ySnodes interpxfinal, yS nodes;
Initialize the error vectors
edx zeroslengthnodes;
edx zeroslengthnodes;
edx zeroslengthnodes;
Calculate the errors
for i :lengthnodes
edxi ySnodesi interp::xend, ydx nodesi;
edxi ySnodesi interp::xend, ydx nodesi;
edxi ySnodesi interp::xend, ydx nodesi;
end
Compute the Euclidean norms of the error vectors
normEdx normedx;
normEdx normedx;
normEdx normedx;
Store the norms in a row vector
normEa normEdx normEdx normEdx;
Display results
dispNorm of Errors:;
dispnormEa;
Errors:
Previous Assessment: of Tests Passed
Is the solution for the Shooting Method correct?
Is the solution for the Finite Difference Method delta correct?
Variable ydx has an incorrect value.
Is the solution for the Finite Difference Method delta correct?
Variable has an incorrect value.
Is the solution for the Finite Difference Method delta correct?
Variable ydx has an incorrect value.
Is the norm of the relative approxmiate error correct?
Variable normEa has an incorrect value.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started