Integrating a 2nd order ODE

3 ビュー (過去 30 日間)
PetronasAMG
PetronasAMG 2021 年 10 月 16 日
回答済み: Star Strider 2021 年 10 月 17 日
I am given an equation,
d^2y/dx^2 + q(x) = 0
x ranges from 0 to 1 and y(0) = 1 and y(L) = 1.5
where L = 1
and q(x) = 2*cos((pi*x)/L)
here is what I have
function dydx = yfunc (x,y)
x = linspace(0,1,30);
L = 1;
for i = 1:length(x)
qx(i)= 2*cos((pi*x(i))/L);
end
dydx= -qx;
end
%main script
[x,y] = ode45(@yfunc,x,[1 1.5]);
I am running into an error stating Dimensions of arrays being concatenated are not consistent. Could you please help me?

採用された回答

Star Strider
Star Strider 2021 年 10 月 17 日
This is a boundary value problem. Use bvp4c to solve it.
syms y(x) x L Y
q(x) = 2*cos(pi*x/L);
Dy = diff(y);
D2y = diff(Dy);
ODE = D2y + q
ODE(x) = 
[VF,Subs] = odeToVectorField(ODE)
VF = 
Subs = 
bvpfcn = matlabFunction(VF, 'Vars',{x,Y,L})
bvpfcn = function_handle with value:
@(x,Y,L)[Y(2);cos((x.*pi)./L).*-2.0]
I solved it completely, however I do not want to deprive you of the same feeling of accomplishment, so I leave the rest to you. It is a straightforward solution. Follow the examples in the documentation I linked to.
.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by