How to call a separate function to a new program

9 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 9 Feb 2024
Edited: Stephen23 on 9 Feb 2024
"This was my attempt, that went poorly."
The basic cause is your continued usage of INPUT everywhere, and hiding the function behind scripts.
I have read through your threads for the last few days and unfortunatately no one has guided you to writing a very basic function in an Mfile.
Once defined, this is how you can call the function from anywhere (i.e. from the command line, from another function, etc, it makes no difference in MATLAB where you call it from):
a_in = 1;
b_in = 2;
c_in = 3;
Discriminant(a_in, b_in, c_in)
The value of our discriminant is: -8
You must define the function as the main function in an Mfile. Do NOT hide the function in a script like you keep doing, with lots of code before the function definition! I repeat, do NOT put any other code in the Mfile, just the function defintion and NOTHING else. This advice needs to be followed exactly: just this code and NOTHING else in your Mfile:
function Discriminant(a,b,c)
d = (b^2)-(4*a*c);
fprintf('The value of our discriminant is: %i \n', d)
end
Make sure that you save the Mfile on the MATLAB Search Path (by far the simplest is to save it in the current directory).
Make sure that you use Discriminant.m for the Mfile name (not strictly required, but makes your life easier).
Then you will be able to call it from the command line, from other functions, etc. What you have been shown in the last day or two does not help you to achieve that goal.
That is how to write a MATLAB function that you can call from the command line or from other functions.
Avoid INPUT. And especially ignore bad advice to fill your functions up with INPUT calls. Ugh.
  2 Comments
TheSaint
TheSaint on 9 Feb 2024
This was very helpful, thanks. However, once my discriminant is calculated, how do I use it in another function?
function Discriminant (a,b, c)
d = 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
So when I input Discriminant (a,b,c) in the command window, I get my Discriminant value, that works, but I need that value to go in function like this to solve for roots, and then print that.
Stephen23
Stephen23 on 9 Feb 2024
I don't follow your code at all. Why do you call the function inside itself?:
function Discriminant (a,b, c)
d = Discriminant(a, b, c);
You call the function with an output argument, but where is that output argument defined inside the function?
Oh, I think I see the problem: you are naming all of your functions with exactly the same name.
Don't do that.
Give your functions (and scripts) different names. Really, otherwise you will just get yourself into a big mess.
Then you can modify your function to return an output argument (rather than printing to the command window):
a_in = 1;
b_in = 2;
c_in = 3;
d_out = MyFunWithAnotherName(a_in, b_in, c_in)
d_out = -8
You can do whatever you want with that output value.
function d = MyFunWithAnotherName(a,b,c)
d = (b^2)-(4*a*c);
end
How to define functions with input and output arguments is explained in the FUNCTION documentation:

Sign in to comment.

More Answers (1)

Shubham
Shubham on 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:
  1. The QuadraticSolver function asks the user for the coefficients a, b, and c.
  2. It then calls the Discriminant function to calculate the discriminant d.
  3. If the discriminant is non-negative, it calculates the two possible roots x1 and x2.
  4. It prints the roots if they are real or notifies the user that there are no real roots if the discriminant is negative.
  5. 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.

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!