solve differential equations use bvp4c
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
lvlv sun
el 3 de Abr. de 2024
Respondida: Sam Chak
el 3 de Abr. de 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)
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 comentarios
Respuesta aceptada
Sam Chak
el 3 de Abr. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Boundary Value Problems en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!