Clear Filters
Clear Filters

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

2 views (last 30 days)
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 Comments
Gani
Gani on 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

Sign in to comment.

Answers (1)

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

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!