Why function displays one result, and how to stop it?

1 次查看(过去 30 天)
Hello. I have made function that calculate roots of quadratic function ax^2 + bx +c, and then create graph for this function.
Also I have some text in function that will be printed to the screen. But also, except this text, function displays ans=(some number - first root).
I want just my text to be displayed. Here is the code:
function [x1, x2] = kvadratna(a, b, c)
D = b^2 - 4*a*c;
x = -10:0.2:10; %x osa za grafik
y = a.*x.^2 + b.*x + c; %funkcija
if(D<0)
fprintf('Funkcija nema realne nule');
elseif(D==0)
x1 = -b/(2*a);
x2=x1;
fprintf('Funkcija ima dvije iste realne nule\n');
fprintf('Te realne nule su: %.2f i %.2f\n',x1,x2);
elseif(D>0)
x1 = ((-b+sqrt(D)) / (2*a));
x2 = ((-b-sqrt(D)) / (2*a));
fprintf('Funkcija ima dvije razlicite realne nule\n');
fprintf('Te realne nule su: %.2f i %.2f\n',x1,x2);
end
plot(x,y,'g')
grid on
xlabel('x')
ylabel('f(x)')
title('CRTANJE FUNKCIJE')
end
and here is what it displays:
probbb.png
I dont want this ans=-1 to be displayed. How can I fix this?
  4 个评论
Gani
Gani 2019-2-21
function [x1, x2] = kvadratna(a, b, c)
D = b^2 - 4*a*c;
x = -10:0.2:10; %x osa za grafik
y = a.*x.^2 + b.*x + c; %funkcija
if(D<0)
fprintf('Funkcija nema realne nule');
elseif(D==0)
x1 = -b/(2*a);
x2=x1;
% fprintf('Funkcija ima dvije iste realne nule\n');
% fprintf('Te realne nule su: %.2f i %.2f\n',x1,x2);
elseif(D>0)
x1 = ((-b+sqrt(D)) / (2*a));
x2 = ((-b-sqrt(D)) / (2*a));
% fprintf('Funkcija ima dvije razlicite realne nule\n');
% fprintf('Te realne nule su: %.2f i %.2f\n',x1,x2);
end
plot(x,y,'g')
grid on
xlabel('x')
ylabel('f(x)')
title('CRTANJE FUNKCIJE')
end
Test.PNG

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2019-2-21
编辑:KSSV 2019-2-21
Call the function as below:
[x1,x2] = kvadratna(1, 6, 5) ;

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by