I want to solve a second order differential equation which passes parameters to the ode solver. The parameters are column arrays and hence I used for loop in the function. I don't know if what I wrote is right or wrong. I am getting the solution. But I have no idea if it is right or wrong..
Is the code within the function right? I mean the syntax...
Thankyou...
The function is given below
function r = f1(tspan,y2,I,J,K,L,W)
r = zeros(2,1);
c = zeros(length(tspan));
r(1) = y2(2);
for n =2:length(tspan)
c(n) = -(I(n).*y2(2) + J(n).*y2(1) + K(n) + L(n).*cos(W*tspan(n)));
end
r(2)=c;

1 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 18 日
And how did you call this function?

サインインしてコメントする。

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 18 日

1 投票

function r = f1(t,y2,tspan1,I,J,K,L,W)
r = zeros(2,1);
c = zeros(length(tspan1));
r(1) = y2(2);
for n =2:length(tspan1)
c(n) = -(I(n).*y2(2) + J(n).*y2(1) + K(n) + L(n).*cos(W*tspan1(n)));
end
r(2)=c;
% How to call your function
% Define Tspan1, Ic and your parameters
[T Y] = ode45(@(t,y2) f1(t,y2,Tspan1,I,J,K,L,W),Tspan1,IC)

3 件のコメント

Vin
Vin 2013 年 10 月 18 日
編集済み: Vin 2013 年 10 月 18 日
This is my function call ( from part of my code)
for n = store2:numel(t)
store1=n
m=n;
if (theta(n)<thetaD)
tspan = store2:0.1:n-1;
init2 = [y1(store2) y1dot(store2)];
[tspan,y2] = ode45(@(tspan,y2) f1(tspan,y2,I,J,K,L,W),tspan,init2);
c2 = vertcat(c2,y2(:,1));
break
end
end
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 18 日
Look at the above answer for how to write the function f1, and call it like below:
[t,y2] = ode45(@(t,y2) f1(t,y2,tspan,I,J,K,L,W),tspan,init2);
Vin
Vin 2014 年 2 月 5 日
I think I got it Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

質問済み:

Vin
2013 年 10 月 18 日

コメント済み:

Vin
2014 年 2 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by