My code below plots the solution of the equation of Mathieu with the initial condition: y(0) = 1, y'(0) = 1
Now I still want the same solution to this problem, but with new boundary conditions: y(1) = -1, y(10) = 1
I tried solving it with dsolve(eq, y(1) == -1, y(10) == 1) but then I couldn't implement it into my function.
syms t y(t)
syms a q
tm = [0 75]; %time intervall
figure(1)
clf
hold on
y0=[-1;1]; %initial conditions
[t,y1] = ode23(@Mathieu, tm, y0);
plot(t,y1(:,2))%y(t)
xlim([0 75])
function dydt = Mathieu(t,y)
a = 2;
q = 0.5;
dydt = [y(2); -(a-2*q*cos(2*t))*y(1)];
end

댓글 수: 5

Ameer Hamza
Ameer Hamza 2020년 5월 9일
Your limits of integrations are [0 75]. However, you specified the conditions at t=1 and t=10. Are you trying to solve a multiple boundary problem?
Borjan Trajanoski
Borjan Trajanoski 2020년 5월 9일
Thanks for the hint, I did not think of that, but the limits are not necessary, I just used them for the initial condition. If I delete them, should I then be able to apply the boundary conditions?
Ameer Hamza
Ameer Hamza 2020년 5월 9일
Yes, you can apply boundary conditions, but you will need to use bvp4c or bvp5c (if multiple boundaries), instead of ode45. If you just care about boundary conditions at [1 10], then see the code in my answer.
Borjan Trajanoski
Borjan Trajanoski 2020년 5월 9일
I just did it with bvp4c, really cool function! And thanks for the additional code :)
Ameer Hamza
Ameer Hamza 2020년 5월 9일
I am glad to be of help.

댓글을 달려면 로그인하십시오.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 9일

0 개 추천

Try this code with bvp4c in the limits [1 10], with the boundary condition given in the question. For more details, see the documentation of bvp4c
tm = [1 10]; %time intervall
t = linspace(tm(1), tm(2), 50);
guess = bvpinit(t, [1; 1]);
sol = bvp4c(@Mathieu, @bcFun, guess);
plot(sol.x, sol.y(1,:)) % y(t) is the row of solution
xlim(tm)
function dydt = Mathieu(t,y) %ODE
a = 2;
q = 0.5;
dydt = [y(2); -(a-2*q*cos(2*t))*y(1)];
end
function res = bcFun(ya, yb) % boundary function
res = [ya(1)+1; % y(1)=-1
yb(1)-1]; % y(10) = 1
end

추가 답변 (1개)

Nagaraja Shamsundar
Nagaraja Shamsundar 2020년 5월 9일

0 개 추천

Your goal is to solve a boundary value problem (BVP). Some BVPs can be converted into equivalent initial value problems (IVP), but in general it is more appropriate to use a BVP solver such as Matlab's BVP4C instead of an IVP solver such as ODE23.
In Matlab, type help bvp4c or doc bvp4c.

댓글 수: 1

Borjan Trajanoski
Borjan Trajanoski 2020년 5월 9일
Thank you! I didn't know there is an extra function for this kind of problem. I am looking it up right now.

댓글을 달려면 로그인하십시오.

카테고리

질문:

2020년 5월 9일

댓글:

2020년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by