Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views1 page

MATLAB7

The document provides MATLAB code for solving second-order differential equations using the ode45 and bvp4c functions. It demonstrates the transformation of a second-order ODE into a system of first-order equations, the implementation of boundary conditions, and the creation of initial guesses for the solution. Various plots are generated to visualize the solutions based on different initial guesses.

Uploaded by

dipanjanbarman79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

MATLAB7

The document provides MATLAB code for solving second-order differential equations using the ode45 and bvp4c functions. It demonstrates the transformation of a second-order ODE into a system of first-order equations, the implementation of boundary conditions, and the creation of initial guesses for the solution. Various plots are generated to visualize the solutions based on different initial guesses.

Uploaded by

dipanjanbarman79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

MATLAB7

Friday, 30 April 2021 2:00 PM

% second order differential equation : y''=(2*y)+(8*x)*(9-x); solve using


% ode45 between 0 and 9, say. To use BVP, see next demo
% Apart from DSOLVE, ode45 can be used by clc;
xmesh = linspace(0,pi/2,10);
% transforming a n-th order ode into a system of n 1st order ode's to solve solinit = bvpinit(xmesh, @guess);
sol1 = bvp4c(@bvpfun, @bcfun, solinit);
% rewrite the given equation as a system of first-order equations. plot(sol1.x, sol1.y, '-o')
% y1'=y2, y2'=2y1+8*x*(9-x) title('BVP with Solutions That Depend on the Initial Guess function');
xlabel('x')
[x,y] = ode45(@fun,[0 9],[0 -28]); ylabel('y')
figure;
plot(x,y) solinit = bvpinit(xmesh, [3 0]);
function dy = fun(x,y) sol2 = bvp4c(@bvpfun, @bcfun, solinit);
plot(sol1.x,sol1.y(1,:),'-o',sol2.x,sol2.y(1,:),'-o')
dy = zeros(2,1); title('BVP with Different Solutions That Depend on the Initial Guess');
xlabel('x')
dy(1) = y(2); ylabel('y')
figure;
dy(2) = 2*y(1)+8*x*(9-x); solinit = bvpinit(xmesh, [1 0]);
sol3 = bvp4c(@bvpfun, @bcfun, solinit);
end plot(sol1.x,sol1.y(1,:),'-o',sol3.x,sol3.y(1,:),'-o')
title('BVP with Different Solutions That Depend on the Initial Guess');
xlabel('x')
ylabel('y')

% Solve boundary value problems for ODEs by collocation. function dydx = bvpfun(x,y) % equation being solved
dydx = [y(2)
% matlab source help at -exp(y(1))];
end
https://in.mathworks.com/help/matlab/ref/bvp4c.html %-------------------------------------------
function res = bcfun(ya,yb) % boundary conditions
res = [ya(1)
% bvp4c(ODEFUN,BCFUN,SOLINIT) integrates a system of ordinary differential end
yb(1)];

% equations of the form y' = f(x,y) on the interval [a,b], subject to general function g = guess(x) % initial guess for y and y'
% two-point boundary conditions of the form bc(y(a),y(b)) = 0 g = [sin(x)
cos(x)];
end

% Solve a second-order BVP second-order equation


% y''+4y=0.
% The equation is defined on the interval [0,pi/2] subject to the boundary conditions
% y(0)=0,
% y(pi/2)=2.
% To solve this equation write a function that represents the equation as a system
% of first-order equations, a function for the boundary conditions, and a function
% for the initial guess. Then the BVP solver uses these
% three inputs to solve the equation.

% Code Equation
% Write a function that codes the equation. Use the substitutions y1=y and
% y2=y'
% to rewrite the equation as a system of first-order equations.

% y1'=y2, y2'=y1''=y''=-4y1
% The corresponding function is
% function dydx = bvpfcn(x,y)
% dydx = zeros(2,1);
% dydx = [y(2)
% -4y(1)];
% end

% Code Boundary Conditions


% Write a function that codes the boundary conditions in the form g(y(a),y(b))=0.
% In this form the boundary conditions are
% y(0)=0,
% y(pi/2)-2=0
% The corresponding function is
% function res = bcfcn(ya,yb)
% res = [ya(1)
% yb(1)-2];
% end

% Create Initial Guess


% Use the bvpinit function to create an initial guess for the solution of the
% equation. Since the equation relates y'' to y, a reasonable guess is that the
% solution involves trigonometric functions. Use a mesh
% of 10 points in the interval of integration. The first and last values in the mesh
% are where the solver applies the boundary conditions.

% The function for the initial guess accepts x as an input and returns a guess for the value of y1 and y2:
% function g = guess(x)
% g = [sin(x)
% cos(x)];
% end

% The code will thus be:


xmesh = linspace(0,pi/2,10);
solinit = bvpinit(xmesh, @guess);
sol = bvp4c(@bvpfcn, @bcfcn, solinit);
plot(sol.x, sol.y, '-o')

function dydx = bvpfcn(x,y) % equation to solve


dydx = zeros(2,1);
dydx = [y(2)
-4*y(1)];
end
%--------------------------------
function res = bcfcn(ya,yb) % boundary conditions
res = [ya(1)
yb(1)-2];
end
%--------------------------------
function g = guess(x) % initial guess for y and y'
g = [sin(x)
cos(x)];
end
clc;
xmesh = linspace(0,pi/2,10);
solinit = bvpinit(xmesh, @guess);
sol1 = bvp4c(@bvpfun, @bcfun, solinit);
plot(sol1.x, sol1.y, '-o')
title('BVP with Solutions That Depend on the Initial Guess function');
xlabel('x')
ylabel('y')
figure;

solinit = bvpinit(xmesh, [3 0]);


sol2 = bvp4c(@bvpfun, @bcfun, solinit);
plot(sol1.x,sol1.y(1,:),'-o',sol2.x,sol2.y(1,:),'-o')
title('BVP with Different Solutions That Depend on the Initial Guess');
xlabel('x')
ylabel('y')
figure;
solinit = bvpinit(xmesh, [1 0]);
sol3 = bvp4c(@bvpfun, @bcfun, solinit);
plot(sol1.x,sol1.y(1,:),'-o',sol3.x,sol3.y(1,:),'-o')
title('BVP with Different Solutions That Depend on the Initial Guess');
xlabel('x')
ylabel('y')

function dydx = bvpfun(x,y) % equation being solved


dydx = [y(2)
-exp(y(1))];
end
%-------------------------------------------
function res = bcfun(ya,yb) % boundary conditions
res = [ya(1)
yb(1)];
end

function g = guess(x) % initial guess for y and y'


g = [sin(x)
cos(x)];
end

You might also like