solve differential equations use bvp4c
31 views (last 30 days)
Show older comments
I use bvp4c to solve equations:
I use 5 points for xmesh, but get 7 points for x in the solution sol. How can I get the same number of points for x in the solution?
xmesh = linspace(0,pi/2,5);
solinit = bvpinit(xmesh, @guess);
sol = bvp4c(@bvpfcn, @bcfcn, solinit)
function dydx = bvpfcn(x,y) % equation to solve
dydx = zeros(2,1);
dydx = [y(2)
-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
0 Comments
Accepted Answer
Sam Chak
on 3 Apr 2024
Hi @lvlv sun
Add these two lines to code as shown below to get the desired number of points for the solution.
numpts = 5; % desired number of points
xmesh = linspace(0,pi/2, numpts);
solinit = bvpinit(xmesh, @guess);
sol = bvp4c(@bvpfcn, @bcfcn, solinit)
%% ----- add these 2 lines -----
x = linspace(0,pi/2, numpts);
y = deval(sol, x)
%% ----- add these 2 lines -----
function dydx = bvpfcn(x,y) % equation to solve
dydx = zeros(2,1);
dydx = [y(2)
-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
0 Comments
More Answers (0)
See Also
Categories
Find more on Boundary Value Problems in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!