Sie sind auf Seite 1von 6

BVP CODE

%set up the boundaries


xlow=0;
xhigh=1;
%prepare for generation of initial trial solution, and the final solution
N=101; % set up the resolution of trial solution
x0 = linspace(xlow, xhigh, N); % meshing of x

% translate the initial guess into the form that bvp4c can recoganize
solinit=bvpinit(x0,[1 2]);

% call bvp4c to solve the ODE BVP defined by "bvpABfunc" with the boundary
conditions defined by "bvpABbcs"
sol = bvp4c(@bvpABfunc,@bvpABbcs,solinit);

% use deval to decompose the solution


y=deval(sol,x0);
% plot the shooting path
plot(x0,y(1,:));
% Example:How to shoot your angry brid?
% Function to define the boundary conditions
function res = bvpABbcs(ya,yb)
res = [ya(1)-1;yb(1)-1.2];
end
% Example:How to shoot your angry brid?
% Function to define the ODE
function dydx = bvpABfunc(x,y)
dydx = [y(2);2*y(2)-3*y(1)+x];
end
% This is an example code to solve an ODE Boundary Value problem
% Completed by Yun Long, NC State University, on August 2012

clear;
clc;
%set up the boundaries
xlow=0;
xhigh=1;
%prepare for generation of initial trial solution, and the final solution
N=101; % set up the resolution of trial solution
x0 = linspace(xlow, xhigh, N); % meshing of x

global beta phi; % Define the parameters are used in other functions

beta = 1;
phi = 10;

% translate the initial guess defined by bvpguess711example into the form


% that bvp4c can recoganize
solinit=bvpinit(x0,@bvpig);

% call bvp4c to solve the ODE BVP defined by "bvpfunc" with the
% boundary conditions defined by "bvpbcs"
sol = bvp4c(@bvpfunc,@bvpbcs,solinit);
% For detailed use of BVP4C and its options, enter "help BVP4C"

% use deval to decompose the solution


y=deval(sol,x0);
% plot the solution
plot(x0,y);
hold off;
function res = bvpbcs(ya,yb)
res = [ya(1)-1; yb(2)];
end

function dydx=bvpfunc(x,y)
% setup the parameters beta and phi
global beta phi;

% define the ODE


dydx = [y(2); phi*y(1)/(1+beta*y(1))];

end

function y0=bvpig(x)
global phi;

sqrtphi=sqrt(phi);
y0(1)=cosh(sqrtphi*(1-x))/cosh(sqrtphi);
y0(2)=-sqrtphi*sinh((1-x)*sqrtphi);
% Example:van der Pol eqns in relaxation oscillation
% Main code
clear; clc;

% Define the time span and initial conditions


tspan=[0 3000];
y1_0=2;
y2_0=0;

% Call ODE45 to Solve the ODE defined by "odeoscfunc"


[T,Y]=ode15s(@odeoscfunc,tspan,[y1_0 y2_0]); plot(T,Y(:,1),'o');

% Example:van der Pol eqns in relaxation oscillation


% Function to define the ODE
function dydt = odeoscfunc(t,y)
dydt = [y(2)
1000*(1 - y(1)^2)*y(2) - y(1)];
% dydt can be a vector form rather than the individual eqn
(i.e.dydt(1)=y(2);dydt(2)=1000*(1 - y(1)^2)*y(2) - y(1))
end
%Example code to use PDEPE function to solve a non-steady-state problem

% where x and t are independent variable (one time-like variable and one

% space-like variable)

%A complete help document can be found by enter "doc pdepe"

%Created specifically from a heat transfer problem on Page 2 of Week 11

% lecture notes

%For teaching andn learning purpose only, written by Yun Long

%Lecturer at National University of Singapore, 2013

function pdepe5010ex

global u;

m = 2;

x = linspace(0,1,20); % set the lower, upper limits and the mesh number

t = linspace(0,2,10); % set the lower, upper limits and the mesh number

sol = pdepe(m,@pdeex1pde,@pdeex1ic,@pdeex1bc,x,t);

% Extract the first component of the solution (similar as for BVP4C)

u = sol(:,:,1);
% Use a surface plot to show the complete solution

surf(x,t,u)

xlabel('Space coordinate x')

ylabel('Time coordinate t')

% A profile at a certain time

figure

plot(x,u(end,:)) % this is to show at the final time

xlabel('Distance x')

ylabel('Solution u') % can change accordingly

% --------------------------------------------------------------

function [c,f,s] = pdeex1pde(x,t,u,DuDx) % define the PDE

alpha = 1.0;

A = 1.0;

B = 2.0; % define system parameters

c = 1.0;

f = DuDx*alpha;

s = A+B*u;

% --------------------------------------------------------------

function u0 = pdeex1ic(x) % define IC

u0 = 5;

% --------------------------------------------------------------

function [pl,ql,pr,qr] = pdeex1bc(xl,ul,xr,ur,t) % define BCs


alpha=1.0;

pl = 0;

ql = 1/alpha;

pr = ur-5;

qr =

Das könnte Ihnen auch gefallen