Answered step by step
Verified Expert Solution
Link Copied!

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 I'm getting. 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. Previous Assessment: 0 of 5 Tests Passed
Is the solution for the Shooting Method correct?
Variable yS must be of size [2011]. It is currently of size [5011]. Check where the variable is assigned a value.
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 y_dx2 has an incorrect value.
Is the solution for the Finite Difference Method delta x=0.05 correct?
Variable ydx3 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
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)];
% First shot
[x1, yz1]= ode45(odesys,[010],[y0 z0_guess1]);
% Second shot
[x2, yz2]= ode45(odesys,[010],[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,[010],[y0 z0_final]);
% Extract the final solution for y
yS = yz_final(:,1);
% PART 2 Electric Boogaloo
% Define the ODE parameters
x_end =10;
y0=3;
y_end =2;
% Define the range of dx values
dx_values =[0.5,0.25,0.05];
% Loop over the dx values
for i =1:length(dx_values)
dx = dx_values(i);
x =0:dx:x_end;
N = length(x);
% Initialize the matrix A and vector b for the linear system Ax = b
A =-2*diag(ones(N,1))+ diag(ones(N-1,1),1)+ diag(ones(N-1,1),-1);
b =4*cos(x)'* dx^2;
% Apply boundary conditions
A(1, :) =0; A(1,1)=1; b(1)= y0;
A(end, :) =0; A(end, end)=1; b(end)= y_end;
% Solve the linear system
y = A\b;
% Store the solution in the specified variable
if dx ==0.5
y_dx1= y;
elseif dx ==0.25
y_dx2= y;
elseif dx ==0.05
y_dx3= y;
end
end
% Define the nodes for comparison
nodes =0:0.5:10;
% Extract the relevant y values n
yS_nodes = interp1(x_final, yS, nodes);
% Initialize the error vectors
e_dx1= zeros(length(nodes),1);
e_dx2= zeros(length(nodes),1);
e_dx3= zeros(length(nodes),1);
% Calculate the errors
for i =1:length(nodes)
e_dx1(i)= yS_nodes(i)- interp1(0:0.5:x_end, y_dx1, nodes(i));
e_dx2(i)= yS_nodes(i)- interp1(0:0.25:x_end, y_dx2, nodes(i));
e_dx3(i)= yS_nodes(i)- interp1(0:0.05:x_end, y_dx3, nodes(i));
end
% Compute the Euclidean norms of the error vectors
normE_dx1= norm(e_dx1,2);
normE_dx2= norm(e_dx2,2);
normE_dx3= norm(e_dx3,2);
% Store the norms in a row vector
normEa =[normE_dx1, normE_dx2, normE_dx3];
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions