I have to solve the following equation using bvp4c. But I cannot understand the error in my code.
where p=1 and boundary conditions are
Please help me to find the error in the code.
p=1;
xmesh = linspace(-1,1,5);
solinit = bvpinit(xmesh,@guess);
sol = bvp4c(@bvpfcn,@bcfcn,solinit);
plot(sol.x,sol.y,'-o');
function dydx = bvpfcn(p,x,y)
dydx = zeros(2,1);
dydx = [y(2)
(-2+8*x^2*y(1))/(p+x^2)^2];
end
function res = bcfcn(p,ya,yb)
res = [ya(1)-(1/(1+p))
yb(1)-(1/(1+p))];
end
function g = guess(x)
g = [sin(x)
cos(x)];
end

 채택된 답변

Star Strider
Star Strider 2021년 9월 25일

0 개 추천

The symbolic Math Toolbox disagrees with you with respecty to ‘bvpfun’, however I don’t know if that was the problem. (I use it because it never makes algebra errors, a problem that continues to plague me.) The only other change I made was to cast all the functions as anonymous functions, and change the order of arguments and the calls to them in bvp4c.
The additional parameters (only one here, that being ’p’) do not always have to be the last argument, however the differential equaation integration functions must only ‘see’ the arguments they are expecting, in the order they are expecting them.
syms p x y(x) Y
Dy = diff(y)
Dy(x) = 
D2y = diff(Dy)
D2y(x) = 
Eqn = (p+x^2)^2 * D2y - 8*x^2*y == -2
Eqn(x) = 
[VF,Subs] = odeToVectorField(Eqn)
VF = 
Subs = 
bvpfcn = matlabFunction(VF, 'Vars',{x,Y,p})
bvpfcn = function_handle with value:
@(x,Y,p)[Y(2);(x.^2.*Y(1).*4.0-1.0).*1.0./(p+x.^2).^2.*2.0]
p = 1;
bvpfcn = @(x,y,p) [y(2); (x.^2+y(1).*4-1).*1./(p+x.^2).^2*2];
bcfcn = @(ya,yb,p) [ya(1)-1/(1+p); yb(1)-1/(1+p)];
guess = @(x) [sin(x); cos(x)];
p=1;
xmesh = linspace(-1,1,5);
solinit = bvpinit(xmesh,guess);
sol = bvp4c(@(x,y)bvpfcn(x,y,p),@(ya,yb)bcfcn(ya,yb,p),solinit);
figure
plot(sol.x,sol.y,'-o')
grid
legend(string(Subs), 'Location','best')
xlabel('x')
ylabel('y(x)')
Experiment to get different results.
.

추가 답변 (0개)

카테고리

제품

릴리스

R2021b

태그

질문:

2021년 9월 25일

답변:

2021년 9월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by