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

1 Ansicht (letzte 30 Tage)
Faris Hajdarpasic
Faris Hajdarpasic am 21 Feb. 2019
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 Kommentare
Gani
Gani am 21 Feb. 2019
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

KSSV
KSSV am 21 Feb. 2019
Bearbeitet: KSSV am 21 Feb. 2019
Call the function as below:
[x1,x2] = kvadratna(1, 6, 5) ;

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by