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.
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