For some reason, I am not getting plots when I put in this code for this question.
5 次查看(过去 30 天)
显示 更早的评论
The question states: insert the plots obtained for the following three cases in the figure below.
In the title of the plot replace “Roots of Polynomial” with your “Last_Name, FirstName”
a. a=1, b=3, c=4
b. a=1, b=4, c=4
c. a=1, b=3, c=2
My code says:
% Enter the real coefficients of a quadratic polynomial
disp('Enter the coeffs of the quadratic: a x^2 + b x + c')
a = input('Enter the coeff. of x^2 term: a = 1');
b = input('Enter the coeff. of x^1 term: b =3 ');
c = input('Enter the coeff. of x^0 term: c =4 ');
% Compute the Discriminant: Delta = b^2 - 4ac
Delta = b^2 - 4*a*c;
% Solve for the roots of the quadratic polynomial
if Delta < 0 % Case of real and distinct roots
x1 = (-b + sqrt(Delta)) / 2*a;
x2 = (-b - sqrt(Delta)) / 2*a;
fprintf(' Real and distinct roots: x1 = %4.2f x2 = %4.2f\n', x1, x2)
elseif Delta == 0 % Case of real repeated roots
x1 = -b / (2*a);
x2 = x1;
fprintf(' Real and repeated roots: x1 = %4.2f x2 = %4.2f\n', x1, x2)
else % Case of complex roots
x1 = (-b + sqrt(Delta)) / (2*a);
x2 = (-b - sqrt(Delta)) / (2*a);
fprintf(' Imag roots: x1 = %g + j%g and x2 = %g + j%g \n',...
real(x1), imag(x1), real(x2), imag(x2));
end
plot = [num2str(a), 'x^2 + ', num2str(b), 'x + ',num2str(c)];
% Plot the two roots
poly(real(x1), imag(x1),'rx', real(x2), imag(x2),'bo', 'MarkerSize',16, 'LineWidth',5)
title(['Roots of polynomial: f(x) = ', poly, ' = 0'], 'FontSize', 16)
xlabel('Real Axis');
ylabel('Imag Axis')
legend(['x_1=',num2str(x1)],['x_2=',num2str(x2)])
grid
Basically, my problem is that I am not getting any graphs and plots from my code. What should I do differently?
1 个评论
Walter Roberson
2023-10-14
plot = [num2str(a), 'x^2 + ', num2str(b), 'x + ',num2str(c)];
That creates a variable named plot that is a character vector. After you execute that line, you will not be able to call plot() as a function until that plot variable is cleared or goes out of scope.
回答(1 个)
Torsten
2023-10-14
编辑:Torsten
2023-10-14
if Delta > 0 % Case of real and distinct roots
x1 = (-b + sqrt(Delta)) / (2*a);
x2 = (-b - sqrt(Delta)) / (2*a);
fprintf(' Real and distinct roots: x1 = %4.2f x2 = %4.2f\n', x1, x2)
instead of
if Delta < 0 % Case of real and distinct roots
x1 = (-b + sqrt(Delta)) / 2*a;
x2 = (-b - sqrt(Delta)) / 2*a;
fprintf(' Real and distinct roots: x1 = %4.2f x2 = %4.2f\n', x1, x2)
And I don't know what you expect from the command
poly(real(x1), imag(x1),'rx', real(x2), imag(x2),'bo', 'MarkerSize',16, 'LineWidth',5)
Maybe you mean
plot(real(x1), imag(x1),'rx', real(x2), imag(x2),'bo', 'MarkerSize',16, 'LineWidth',5)
But this will only plot 2 points. A graph of the parabola together with the roots/root/non-existing real roots would be helpful.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!