How to call a separate function to a new program
Ältere Kommentare anzeigen
a = input('Enter the value of a: ');
b = input('Enter the value of b: ');
c = input('Enter the value of c: ');
Discriminant(a,b,c );
function Discriminant (a,b, c)
d = (b^2)-(4*a*c);
fprintf('The value of our discriminant is: %i \n', d)
end
I have this function that calculates the discriminant of a quadratic equation based on user inputs. However, I need to put this function inside another function (in a separate .m file) that looks like this
a = input('Enter the value of a: ');
b = input('Enter the value of b: ');
c = input('Enter the value of c: ');
function Discriminant (a,b, c)
x1 = ((-b)+sqrt(d))/(2*a)
x2 = ((-b)-sqrt(d))/(2*a)
fprintf('The root values for the given values are x = %1.0f ', x1, 'and x = %1.0f', x2)
end
This was my attempt, that went poorly. I would like it to take the values of a,b,c and use my discriminant function to solve for d, then finally solve for x1 and x2.
Akzeptierte Antwort
Weitere Antworten (1)
Shubham
am 9 Feb. 2024
Hi TheSaint,
To incorporate the Discriminant function within another function that calculates the roots of a quadratic equation, you need to make sure that the Discriminant function is properly defined and called within the new function. Here's how you can structure your code in the separate .m file:
function QuadraticSolver
% Request user input for coefficients
a = input('Enter the value of a: ');
b = input('Enter the value of b: ');
c = input('Enter the value of c: ');
% Calculate the discriminant
d = Discriminant(a, b, c);
% Check if the discriminant is non-negative before calculating roots
if d >= 0
x1 = ((-b) + sqrt(d)) / (2 * a);
x2 = ((-b) - sqrt(d)) / (2 * a);
fprintf('The root values for the given values are x1 = %1.0f and x2 = %1.0f\n', x1, x2);
else
fprintf('The equation has no real roots.\n');
end
end
function d = Discriminant(a, b, c)
% Calculate the discriminant
d = (b^2) - (4 * a * c);
fprintf('The value of our discriminant is: %i\n', d);
end
Here's what the code does:
- The QuadraticSolver function asks the user for the coefficients a, b, and c.
- It then calls the Discriminant function to calculate the discriminant d.
- If the discriminant is non-negative, it calculates the two possible roots x1 and x2.
- It prints the roots if they are real or notifies the user that there are no real roots if the discriminant is negative.
- The Discriminant function is defined within the same file and calculates the discriminant of the quadratic equation.
When you run the QuadraticSolver function, it will perform the entire process of calculating the discriminant and the roots, if they exist. Remember that the above code assumes that a is not zero, as the equation would not be quadratic otherwise. Also, %1.0f in the fprintf function will round off the roots to the nearest integer, you might want to use %f for floating-point precision if that's preferred.
Kategorien
Mehr zu Discriminant Analysis finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!