Error in bvp4c

9 views (last 30 days)
Md. Fahad Rahman
Md. Fahad Rahman on 25 Sep 2021
Answered: Star Strider on 25 Sep 2021
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

Accepted Answer

Star Strider
Star Strider on 25 Sep 2021
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.
.

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!