solve differential equations use bvp4c
35 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Sam Chak
le 3 Avr 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Boundary Value Problems dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!