So this is my function
function v = sdof(t, x)
k=1000;m=5;z=[0 0.1 0.25 0.5 0.75 0.9 1]; c=2*sqrt(k*m)*z;
v=[x(2); x(1).*-k/m+x(2).*-c/m];
and im calling it like this
ts=[0 5] ;
x0=[0.05;0];
[t,x]=ode45('sdof',ts, x0);
figure (1)
plot(t, x(:,1));
I am trying to solve the diffy q with the varying amount of zeta 'z' values. I can get this code to work fine with 1 z value but as soon as i add the array it gives me a bunch of errors. I tried using some type of loop but that didnt work either. Any idea on how to get that to work? Thank you!

 採用された回答

Star Strider
Star Strider 2020 年 3 月 21 日

0 投票

Loop through the values of ‘z’:
sdof = @(t, x, k, m, z) [x(2); x(1).*-k/m+x(2).*-2*sqrt(k*m)*z/m];
k=1000;
m=5;
z=[0 0.1 0.25 0.5 0.75 0.9 1]
% and im calling it like this
ts=linspace(0, 5, 150);
x0=[0.05;0];
for kz = 1:numel(z)
[t,x{kz}]=ode45(@(t,x)sdof(t, x, k, m, z(kz)),ts, x0);
end
xm = cell2mat(x);
figure (1)
plot(t, xm(:,1:2:end));
legend(sprintfc('z = %.2f', z))

4 件のコメント

ravensrgr8
ravensrgr8 2020 年 3 月 21 日
Wow your awesome! Thank you! So it looks like you eliminated the function? I think I had tried something like that with the loop but I was putting the loop in the function. What is the @ symbol doing? Just so I understand the code. Also what is the cell2mat doing? I have never seen that before.
Star Strider
Star Strider 2020 年 3 月 21 日
Thank you! As always, my pleasure!
‘So it looks like you eliminated the function?’
I changed the function to an anonymous function. See the documentation on Anonymous Functions for a complete discussion of them.
‘What is the @ symbol doing?’
The @ symbol creates a function handle (as used here). See the documentation on What Is a Function Handle? for more information (although that is partially explained in the documentation on anonymous functions as well).
‘Also what is the cell2mat doing?’
I created a cell array for ‘x’ because it makes the addressing easier in the loop. The cell2mat function creates a matrix from it. Note that this will only work if the individual cells in the cell2mat argument are the same sizes. I used:
ts=linspace(0, 5, 150);
to be certain that they all are.
ravensrgr8
ravensrgr8 2020 年 3 月 21 日
Perfect! Thank so much! Stay safe and healthy in these crazy times!
Star Strider
Star Strider 2020 年 3 月 21 日
Thank you! As always, my pleasure! And I hope you will as well!

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

その他の回答 (0 件)

カテゴリ

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

タグ

質問済み:

2020 年 3 月 20 日

編集済み:

2020 年 3 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by