Plot equation using user input array

조회 수: 2 (최근 30일)
Daniel
Daniel 2023년 8월 3일
답변: Mrutyunjaya Hiremath 2023년 8월 3일
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
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
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:
  1. 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.
  2. Added a missing parenthesis in the transfer function Gs.
  3. 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].

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by