Matlab code not computing

2 views (last 30 days)
lateef
lateef on 18 Jul 2023
Commented: Image Analyst on 21 Jul 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 Comments
Stephen23
Stephen23 on 18 Jul 2023
Edited: Stephen23 on 18 Jul 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 on 18 Jul 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');

Sign in to comment.

Accepted Answer

Alan Stevens
Alan Stevens on 18 Jul 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 Comments
lateef
lateef on 18 Jul 2023
yes thank you for the help
Image Analyst
Image Analyst on 21 Jul 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!