Matlab code not computing

1 visualización (últimos 30 días)
lateef
lateef el 18 de Jul. de 2023
Comentada: Image Analyst el 21 de Jul. de 2023
is anyone able to review my code and tell me why its not outputing correcrtly when i run it im not sure what errors i have
clear
p=-1;
z=-5;
K = 0.05:0.001:100;
a=K;
b=2*K.^2-p*K;
c=-z*K.^2;
root1 = (-b + sqrt(b.^2 - 4.*a.*c))./(2.*a);
root2 = (-b - sqrt(b.^2 - 4.*a.*c))./(2.*a);
root = root1;
K_corresponding = K;
decay_rate = real;
fprintf('The maximal imaginary part is: %f\n', max_imag_part);
fprintf('The corresponding value of K is: %f\n', K_corresponding);
fprintf('The decay rate is: %f\n', decay_rate);
figure
plot(real(root), imag(root));
scatter[real(root(max_index)], imag[root(max_index)], 'red'; 'filled');
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
xlabel('Real Part');
ylabel('Imaginary Part');
title('Root Locus Plot');
  2 comentarios
Stephen23
Stephen23 el 18 de Jul. de 2023
Editada: Stephen23 el 18 de Jul. de 2023
scatter[real(root(max_index)], imag[root(max_index)], 'red'; 'filled');
% ^ ^ should be parentheses
% ^ what is this for?
% ^ should be parenthesis, not square bracket
lateef
lateef el 18 de Jul. de 2023
even afiter put in paranthesis my code still computes an error
scatter((real(root(max_index)), imag(root(max_index)), 'red'; 'filled';
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
xlabel('Real Part');

Iniciar sesión para comentar.

Respuesta aceptada

Alan Stevens
Alan Stevens el 18 de Jul. de 2023
The use of square brackets in the scatter function is not the only problem! Are you looking for something like this?
p=-1;
z=-5;
K = 0.05:0.001:100;
a=K;
b=2*K.^2-p*K;
c=-z*K.^2;
root1 = (-b + sqrt(b.^2 - 4.*a.*c))./(2.*a);
root2 = (-b - sqrt(b.^2 - 4.*a.*c))./(2.*a);
root = root1;
iroot = imag(root);
maximag = max(iroot);
max_index = find(iroot == maximag);
K_corresponding = K(max_index);
decay_rate = real(root(max_index));
fprintf('The maximal imaginary part is: %f\n',maximag);
The maximal imaginary part is: 1.936492
fprintf('The corresponding value of K is: %f\n', K_corresponding);
The corresponding value of K is: 2.000000
fprintf('The decay rate is: %f\n', decay_rate);
The decay rate is: -2.500000
figure
plot(real(root), imag(root));
hold on
scatter(real(root(max_index)), imag(root(max_index)), 'red', 'filled');
xlabel('Real Part');
ylabel('Imaginary Part');
title('Root Locus Plot');
  2 comentarios
lateef
lateef el 18 de Jul. de 2023
yes thank you for the help
Image Analyst
Image Analyst el 21 de Jul. de 2023
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by