Question

I need to create a MATLAB function, bvp_solve.m, to approximate the solution y(x). The function takes the number of grid points n as an input. The outputs are grid vector x and the solution vector y

ODE: y -y(x - 4) sin(x)+(1 )cos(x) for E [0, 1 BC: y(0) 0, /(0) = -1 (1)sin(1)

%% This is the function i have so far:

function [xi, yi] = bvp_solve(n)
% BVP_SOLVE computes the solution y(x) of a two-point boundary value problem
% using finite difference method (FDM).
% The governing equation is
% y''' = -y + (x - 4) sin(x) + (1 - x)cos(x)
% Boundary conditions: y(0) = 0
% y'(0) = -1
% y'(L) = sin(1)
% The input n is number of grid points used in FDM. The outputs are grid
% vector xi and the solution vector yi.
% Call format: [xi, yi] = bvp_solve(n)

%% Setup grid
L = 1;
h = 1/(n-1);
xi = linspace(0, L, n)';
y3 = @(x, y) -y + (x-4)*sin(x) + (1-x)*cos(x);

%% Construct the linear system M*y = rhs
% Matrix M and rhs
M = zeros(n, n);
rhs = zeros(n,1);

for i = 2:(n-1)
M(i,i-1) = ...;
M(i,i+1) = ...;
M(i,i) = ...;
rhs(i) = ...;
end
%% Enforce boundary conditions:
% y(0) = 0
rhs(1) = 0;
% y'(0) = -1
% y'(1) = sin(1)

%% Solve for yi's:
yi = M\rhs;

end % function bvp_solver

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Matlab code:

function [x, y] = ode_three(n)

x = 0:1/n:1;
init = [0 -1 sin(1)];%initial conditions

[x, y] = ode45(@RK4SYSTEM, x, init);%function call to solve
  
function dydt = RK4SYSTEM(x,y)
dydt = zeros(3,1);
dydt(1) = y(2);
dydt(2) = y(3);
dydt(3) = -y(1)+(x-4)*sin(x)+(1-x)*cos(x);
end
disp(y)
disp(x)
  
end

function [x, yl ode three (n) 1 2 0:1/n: 1 [0 -1 sin (1)1;%initial conditions X init 4 _ [x, у] ode45 (@RK4SYSTEM, x, init);%

First column of y is y(x)

Second column of y is y'(x)

Third column of y is y''(x)

Add a comment
Know the answer?
Add Answer to:
I need to create a MATLAB function, bvp_solve.m, to approximate the solution y(x). The function takes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT