Hi guys, this my quadratic equation i'm having problem to plot. it doesn't plot anything, I need a hand
2 ビュー (過去 30 日間)
古いコメントを表示
clc
clear all;
close all;
syms x
x=-10:1:10;
a=input( ' Enter the A value \n' );
if a == 0
disp(' A is = 0');
end
b=input( ' Enter the B value \n' );
c=input( ' Enter the c value \n' );
dis = sym(sqrt(b^2 -4*a*c));
x1=(-b+dis) /(2*a);
x2=(-b-dis) /(2*a);
sym(x1)
sym(x2)
plot(x,x1);
hold on
plot(x,x2);
1 件のコメント
Image Analyst
2015 年 12 月 28 日
x is a multi-element vector while x1 and x2 are single numbers. Exactly what are you trying to plot? The quadratic curve plus the roots?
回答 (1 件)
Image Analyst
2015 年 12 月 28 日
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Define x axis.
x=-10:1:10;
% Get user values of the coefficients.
a=input( ' Enter the A value (e.g. -1) ' );
if a == 0
disp(' A is = 0');
return;
end
b=input( ' Enter the B value (e.g. 2) ' );
c=input( ' Enter the c value (e.g. 55) ' );
% Create the y values, the curve.
y = a * x.^2 + b * x + c;
% Plot the quadratic curve.
plot(x, y, 'b*-', 'MarkerSize', 20, 'LineWidth', 3);
grid on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Solve for the roots.
dis = sqrt(b^2 -4*a*c)
x1 = (-b + dis) /(2*a)
x2 = (-b - dis) /(2*a)
% Plot the roots
hold on;
plot(x1, 0, 'ro', 'MarkerSize', 20, 'LineWidth', 3);
plot(x2, 0, 'ro', 'MarkerSize', 20, 'LineWidth', 3);
% Plot the axes
xl = xlim();
yl = ylim();
line([0,0], yl, 'LineWidth', 3, 'Color', 'k');
line(xl, [0,0], 'LineWidth', 3, 'Color', 'k');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Line Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!