solve differential equations use bvp4c

35 vues (au cours des 30 derniers jours)
lvlv sun
lvlv sun le 3 Avr 2024
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)
sol = struct with fields:
solver: 'bvp4c' x: [0 0.1963 0.3927 0.7854 1.1781 1.3744 1.5708] y: [2x7 double] yp: [2x7 double] stats: [1x1 struct]
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

Réponse acceptée

Sam Chak
Sam Chak le 3 Avr 2024
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)
sol = struct with fields:
solver: 'bvp4c' x: [0 0.1963 0.3927 0.7854 1.1781 1.3744 1.5708] y: [2x7 double] yp: [2x7 double] stats: [1x1 struct]
%% ----- add these 2 lines -----
x = linspace(0,pi/2, numpts);
y = deval(sol, x)
y = 2x5
0 0.7654 1.4142 1.8477 2.0000 2.0000 1.8478 1.4142 0.7654 0.0001
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% ----- 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

Plus de réponses (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by