Plot equation using user input array
조회 수: 2 (최근 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!