Answered step by step
Verified Expert Solution
Link Copied!

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 I'm encountering.Consider the following ODE:
d2ydx2+x2dydx+y=4cos(x), for ,0x10, with ,y(0)=3, and ,y(10)=2
Write a script that:
named ys.
finite difference approximations in your formulation.
b) Solve for y(x) using the finite difference method with x=0.5. Save result as a column vector named ydx.
c) Solve for y(x) using the finite difference method with x=0.25. Save result as a column vector named ydx2.
d) Solve for y(x) using the finite difference method with x=0.05. Save result as a column vector named ydx3.
e) Compare the accuracy of your results for (b),(c), and (d) by doing the following:
vectors of lona values. Note this means you will need to extract only the relevant y values (corresponding to the specified nodes) from your solutions for (b),(c) and (d) to use in this calculation.
Here is my code:
% Shooting Method
% Initial conditions
y0=3;
z0_guess1=0.1; % First guess for the slope at x=0
z0_guess2=0.2; % Second guess for the slope at x=0
% Define the system of ODEs within the script
odesys = @(x, yz)[yz(2); 4*cos(x)- x^2*yz(2)- yz(1)];
% Specify the x values where the solution is to be evaluated
x_eval = linspace(0,10,201); %201 points from 0 to 10
% First shot
[x1, yz1]= ode45(odesys, x_eval, [y0 z0_guess1]);
% Second shot
[x2, yz2]= ode45(odesys, x_eval, [y0 z0_guess2]);
% Interpolate to find a better guess for z(0)
z0_final = interp1([yz1(end,1) yz2(end,1)],[z0_guess1 z0_guess2],2, 'linear', 'extrap');
% Final shot with interpolated initial slope
[x_final, yz_final]= ode45(odesys, x_eval, [y0 z0_final]);
% Extract the final solution for y
yS = yz_final(:,1); %201 elements
% Part 2
function h = solve_finite_difference(dx)
% Constants
N =1; K =1; hbar =-6; h0=3; hn =2; xspan =[0,10];
% Finite difference method
RHS =-N*dx^2/K/hbar;
% Internal nodes
n_nodes = xspan(end)/dx -1;
b = RHS*ones(n_nodes,1);
b(1)= b(1)- h0;
b(end)= b(end)- hn;
A =1*diag(ones(n_nodes-1,1),-1)-2*diag(ones(n_nodes,1),0)+1*diag(ones(n_nodes-1,1),1);
h = A\b;
h =[h0; h; hn];
end
% Solve the BVP using the finite difference method with different step sizes
y_dx1= solve_finite_difference(0.5);
y_dx2= solve_finite_difference(0.25);
y_dx3= solve_finite_difference(0.05);
function normEa = compare_accuracy(ys, y_dx1, y_dx2, y_dx3, delta_x1, delta_x2, delta_xa)
% x_output specifies the points where ys is calculated
x_output =0:0.05:10;
% Extract relevant y values from finite difference solutions
% Interpolate y_dx1 to x_output points
x1=0:delta_x1:10;
y1_interp = interp1(x1, y_dx1, x_output);
% Interpolate y_dx2 to x_output points
x2=0:delta_x2:10;
y2_interp = interp1(x2, y_dx2, x_output);
% Interpolate y_dx3 to x_output points
xa =0:delta_xa:10;
ya_interp = interp1(xa, y_dx3, x_output);
% Calculate the relative errors
% Relative error for delta_x1
err1= abs((ys - y1_interp)./ ys);
% Relative error for delta_x2
err2= abs((ys - y2_interp)./ ys);
% Relative error for delta_xa
erra = abs((ys - ya_interp)./ ys);
% Calculate the Euclidean norms of the error vectors
normEa =[norm(err1), norm(err2), norm(erra)];
end
% Calculate the Euclidean norms of the errors for different delta_x values
normEa = compare_accuracy(yS, y_dx1, y_dx2, y_dx3,0.5,0.25,0.05)
% Display results
yS
y_dx1
y_dx2
y_dx3
Errors:
Previous Assessment: 1 of 5 Tests Passed
Is the solution for the Shooting Method correct?
Is the solution for the Finite Difference Method delta x=0.5 correct?
Variable y_dx1 has an incorrect value.
Is the solution for the Finite Difference Method delta x=0.25 correct?
Variable yddx2 has an incorrect value.
Is the solution for the Finite Difference Method delta x=0.05 correct?
Variable y_dx3 has an incorrect value.
Is the norm of the relative approxmiate error correct?
Variable normEa has an incorrect value.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

These are another 2 more question related to yesterday's problem.

Answered: 1 week ago