# Plot equation using user input array

4 views (last 30 days)
Daniel on 3 Aug 2023
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
Dyuman Joshi on 3 Aug 2023
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.

Mrutyunjaya Hiremath on 3 Aug 2023
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
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].

### Categories

Find more on Fixed-Point Designer in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!