Matlab code not computing

조회 수: 1 (최근 30일)
lateef
lateef 2023년 7월 18일
댓글: Image Analyst 2023년 7월 21일
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
Stephen23
Stephen23 2023년 7월 18일
편집: Stephen23 2023년 7월 18일
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 2023년 7월 18일
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');

댓글을 달려면 로그인하십시오.

채택된 답변

Alan Stevens
Alan Stevens 2023년 7월 18일
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
lateef
lateef 2023년 7월 18일
yes thank you for the help
Image Analyst
Image Analyst 2023년 7월 21일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by