How to use bvp4c for boundary value problem with 2 unknown parameters?

I have written the following code to solve the following coupled equations using bvp4c.
f′′− (a^2)*f = a*RaT*g
g′′− (a^2)*g = a*f
with x=0,1 f=0 g=0
where 'a' and 'Rat' are unknown parameters
errors
Reference to non-existent field 'r'.
Error in bvp_4 (line 7)
fprintf('For r=%f a= %f \n',sol.r(1),sol.r(2));
%-------------
clear all;
r=[5 1]; %initial guess value for unknowns
options=[];%dummy parameter
x=linspace(0,1,50);
solinit=bvpinit(linspace(0,1,10),ones(4,1),r);
sol=bvp4c(@frhs,@bvpbc,solinit,options);
fprintf('For r=%4.2f a= %4.2f \n',sol.r(1),sol.r(2));
bs=deval(sol,x);
hold on
plot(x,bs(1,:));
plot(x,bs(3,:),'*');
for i=1:4
sol=bvp4c(@frhs,@bvpbc,sol,options);
fprintf('For r=%4.2f || a= %4.2f \n',sol.r(1),sol.r(2));
bs=deval(sol,x);
plot(x,bs(1,:));
plot(x,bs(3,:),'*');
end
%converting to first order equations
function rhs=frhs(x,y,r)
rhs=[y(2)
r(2)*r(2)*y(1)+r(2)*r(1)*y(3)
y(4)
r(2)*r(2)*y(3)+r(2)*y(1) ];
end
%boundary conditions
function bc=bvpbc(yl,yr,r)
bc=[yl(1)
yl(3)
yl(2)-1
yr(1)
yr(3)
yr(4)-1];
end
%additional conditions applied 'yr(4)-1' and 'yl(2)-1 ' for two parameters

 Accepted Answer

fprintf('For r=%4.2f a= %4.2f \n',sol.parameters);
Best wishes
Torsten.

More Answers (1)

I don't know where is a mistake( Can you help me?
function dxdt = mat4ode(t,x,mu) % equation being solved
alfa = 0;
dxdt = [x(2)
-x(1)+0.1*(alfa*cos(2*t)*x(1)+x(1)^3)+mu*sin(2*t)+(1-pi^2)*sin(pi*t)+1-0.1*((sin(pi*t)+1)^3)-2*sin(2*t)];
end
%-------------------------------------------
function res = mat4bc(xa,xb,mu) % boundary conditions
res = [xa(1)-1
xb(1)-1
xa(2)-xb(2)];
end
%-------------------------------------------
function yinit = mat4init(t) % initial guess function
yinit = [sin(pi*t)+1
pi*cos(pi*t)
];
end
mu=2;
solinit = bvpinit(linspace(0,2,20),@mat4init,mu);
sol = bvp4c(@mat4ode, @mat4bc, solinit);
fprintf('Fourth eigenvalue is approximately %7.3f.\n',...
sol.parameters)
xint = linspace(0,2);
Sxint = deval(sol,xint);
plot(xint,Sxint)
axis([0 2 -4 4])
title('Eigenfunction of Mathieu''s Equation.')
xlabel('x')
ylabel('y')
legend('y','y''')

Categories

Find more on Loops and Conditional Statements 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!