Error in bvp4c
9 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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)
D2y = diff(Dy)
Eqn = (p+x^2)^2 * D2y - 8*x^2*y == -2
[VF,Subs] = odeToVectorField(Eqn)
bvpfcn = matlabFunction(VF, 'Vars',{x,Y,p})
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 Comments
More Answers (0)
See Also
Categories
Find more on Error Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!