Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
Faris Hajdarpasic
Faris Hajdarpasic el 21 de Feb. de 2019
Comentada: Faris Hajdarpasic el 21 de Feb. de 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 comentarios
Gani
Gani el 21 de Feb. de 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

Iniciar sesión para comentar.

Respuestas (1)

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

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by