Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help with this matlab problem. I have attached my code and the errors I ' m getting. Consider the following ODE: d 2
I need help with this matlab problem. I have attached my code and the errors Im getting. 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. Previous Assessment: of Tests Passed
Is the solution for the Shooting Method correct?
Variable yS must be of size It is currently of size Check where the variable is assigned a 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 solution for the Finite Difference Method delta correct?
Variable has an incorrect value.
Is the norm of the relative approxmiate error correct?
Variable normEa has an incorrect value.
Here is the 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;
First shot
x yz odeodesysy zguess;
Second shot
x yz odeodesysy zguess;
Interpolate to find a better guess for z
zfinal interpyzend yzendzguess zguess 'linear', 'extrap';
Final shot with interpolated initial slope
xfinal, yzfinal odeodesysy zfinal;
Extract the final solution for y
yS yzfinal:;
PART Electric Boogaloo
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 and vector b for the linear system Ax b
A diagonesN diagonesN diagonesN;
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 n
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;
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