Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I desperatley need help with matlab problem. I have attached my code and the current errors I ' m encountering.Consider the following ODE: d 2
I desperatley need help with matlab problem.
I have attached my code and the current 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.
Here is my code:
Shooting Method
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
function h solvefinitedifferencedx
Constants
N ; K ; hbar ; h; hn ; xspan ;
Finite difference method
RHS NdxKhbar;
Internal nodes
nnodes xspanenddx ;
b RHSonesnnodes,;
b b h;
bend bend hn;
A diagonesnnodesdiagonesnnodes,diagonesnnodes;
h Ab;
h h; h; hn;
end
Solve the BVP using the finite difference method with different step sizes
ydx solvefinitedifference;
ydx solvefinitedifference;
ydx solvefinitedifference;
function normEa compareaccuracyys ydx ydx ydx deltax deltax deltaxa
xoutput specifies the points where ys is calculated
xoutput ::;
Extract relevant y values from finite difference solutions
Interpolate ydx to xoutput points
x:deltax:;
yinterp interpx ydx xoutput;
Interpolate ydx to xoutput points
x:deltax:;
yinterp interpx ydx xoutput;
Interpolate ydx to xoutput points
xa :deltaxa:;
yainterp interpxa ydx xoutput;
Calculate the relative errors
Relative error for deltax
err absys yinterp ys;
Relative error for deltax
err absys yinterp ys;
Relative error for deltaxa
erra absys yainterp ys;
Calculate the Euclidean norms of the error vectors
normEa normerr normerr normerra;
end
Calculate the Euclidean norms of the errors for different deltax values
normEa compareaccuracyyS ydx ydx ydx
Display results
yS
ydx
ydx
ydx
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