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!

 Risposta accettata

Star Strider
Star Strider il 21 Mar 2020
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 Commenti

ravensrgr8
ravensrgr8 il 21 Mar 2020
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 il 21 Mar 2020
Perfect! Thank so much! Stay safe and healthy in these crazy times!
Star Strider
Star Strider il 21 Mar 2020
Thank you! As always, my pleasure! And I hope you will as well!

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by