Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

How to correctly use types in a function and be able to choose the type of output

1 visualización (últimos 30 días)
This is my matlab code for a function to find the zeros of a cubic derivative. No matter what type I run I get the same answer. What am I doing wrong?
function [output]=rootspol(A,B,C,type)
if type==0
% Ax^2+Bx+C
if A==0
disp('This is not a quadratic homie. The solution would be undefined')
if B==0
disp('Still not got that quadratic flavor. Would have complex roots')
end
end
end
if B^2<sqrt(3*A*C)
disp('complex roots only')
else
x1=(-B+sqrt(B^2-3*A*C))/(3*A);
x2=(-B-sqrt(B^2-3*A*C))/(3*A);
output=[x1 x2];
end
if type==1
if A==0
disp('This is not a quadratic homie. The solution would be undefined')
end
if B==0
disp('Still not got that quadratic flavor. Would have complex roots')
else
end
if B^2<sqrt(3*A*C)
disp('complex roots only')
else
xa=C/(-B+sqrt(B^2-3*A*C));
xb=C/(-B-sqrt(B^2-3*A*C));
output=[xa xb];
end
if type==2
else A==0
disp('This is not a quadratic homie. The solution would be undefined')
end
if B==0
disp('Still not got that quadratic flavor. Would have complex roots')
else
end
end
if B^2<sqrt(3*A*C)
disp('complex roots only')
else
xa=C/(-B+sqrt(B^2-3*A*C));
xb=C/(-B-sqrt(B^2-3*A*C));
%Showing that x1 and x2 = xa xb
%x1=xb
%(-B+sqrt(B^2-3*A*C))/(3*A)*(-B-sqrt(B^2-3*A*C))/(-B-sqrt(B^2-3*A*C))
%(B^2-(B^2-3*A*C))/((3*A)*(-B-sqrt(B^2-3*A*C)))
%C/(-B-sqrt(B^2-3*A*C)
%x2=xa
%(-B+sqrt(B^2-3*A*C))/(3*A)*(-B+sqrt(B^2-3*A*C))/(-B+sqrt(B^2-3*A*C))
%(B^2-(B^2-3*A*C))/((3*A)*(-B+sqrt(B^2-3*A*C)))
%C/(-B+sqrt(B^2-3*A*C)
x1=(-B+sqrt(B^2-3*A*C))/(3*A);
x2=(-B-sqrt(B^2-3*A*C))/(3*A);
output=[xa xb x1 x2];
%extra credit
if type==3
if A==0
disp('This is not a quadratic homie. The solution would be undefined')
end
if B==0
disp('Still not got that quadratic flavor. Would have complex roots')
else
end
end
if B^2<sqrt(3*A*C)
disp('complex roots only')
else
if B<0
x1=(-B+sqrt(B^2-3*A*C))/(3*A);
x2=C/(-B+sqrt(B^2-3*A*C));
output=[x1,x2];
else
x1=C/(-B-sqrt(B^2-3*A*C));
x2=(-B-sqrt(B^2-3*A*C))/(3*A);
output=[x1 x2];
end
end
end

Respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by