Plot equation using user input array
1 次查看(过去 30 天)
显示 更早的评论
Im trying to seperately plot a unit step response with user input variables in arrays, but can't get the code to work.
Any help is appreciated, thank you.
clear
s = tf('s')
zeta = input('Enter damping ratio values in 2D array: ')
omega = input('Enter natural frequency values in 2D array:')
figure
for n = 1 : length(omega)
n = 1 : length(zeta)
Gs = omega(n)^2 / [(s^2)+(2*zeta(n)*omega(n)*s)+omega(n)^2]
subplot(2,2,n)
step(Gs,10)
title({'Unit Step Response of G(s) = \omega_{n}/(s^2+2\zeta\omega_{n}s+\omega_{n}^2';'\zeta = 0.4 and \omega = 0.5, 1, 2, 4'})
ylabel('System Response')
end
I get this output:
finalproject
s =
s
Continuous-time transfer function.
Model Properties
Enter damping ratio values in 2D array: [1 2]
zeta =
1 2
Enter natural frequency values in 2D array:[1 2]
omega =
1 2
n =
1 2
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a
scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.
Error in finalproject (line 8)
Gs = omega(n)^2 / [(s^2)+(2*zeta(n)*omega(n)*s)+omega(n)^2]
finalproject
s =
s
1 个评论
Dyuman Joshi
2023-8-3
for n = 1 : length(omega)
n = 1 : length(zeta)
You are overwriting n after defining it as the loop variable.
Now n is a vector, so is omega(n) and you can not use ^ for a vector, which is what the error is stating as well.
You should use different variable names.
回答(1 个)
Mrutyunjaya Hiremath
2023-8-3
clear
s = tf('s');
zeta = input('Enter damping ratio values in a 1D array: ');
omega = input('Enter natural frequency values in a 1D array: ');
figure;
for n = 1 : length(omega)
Gs = omega(n)^2 / ((s^2) + (2*zeta(n)*omega(n)*s) + omega(n)^2);
subplot(2, 2, n);
step(Gs, 10);
title({['Unit Step Response of G(s) = \omega_{n}/(s^2 + 2\zeta\omega_{n}s + \omega_{n}^2)'], ...
['\zeta = ', num2str(zeta(n)), ' and \omega_n = ', num2str(omega(n))]});
ylabel('System Response');
end
Errors and Changes Made:
- The variable n is redefined as a vector in the loop (n = 1 : length(zeta)), which would cause issues with the loop iteration. I changed the variable name to n and used it correctly for the loop.
- Added a missing parenthesis in the transfer function Gs.
- Updated the title of each subplot to display the current values of zeta and omega.
Example: use zeta = [0.2, 0.4, 0.6, 0.8] and omega = [1, 2, 4, 6].
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!