solve differential equations use bvp4c
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
lvlv sun
am 3 Apr. 2024
Beantwortet: Sam Chak
am 3 Apr. 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 Kommentare
Akzeptierte Antwort
Sam Chak
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Boundary Value Problems finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!