I'm trying to have a function receive input in 2 different ways.

2 views (last 30 days)
Hi I have a function that determines if 2 lines intersect, are parallel or have infinitely many solutions. I'm able to prompt the user for inputs individually or have them enter the name of the function IPF(slope1,intercept1,slope2,intercept2). If I edit the code I can have it work both ways. However, my professor would like it if the user could decide how they would like to enter the function ( either on one line or being prompted by the function) I have used, if statements, while states, etc, however I have been unable to have a user choose how they want to enter the function without errors.
Here's the current code
function[output]= IPF(m1,b1,m2,b2)
prompt1='Enter your slope value for your first function.\n';
prompt2='Enter your y-intercept value for your first function.\n';
prompt3='Enter your slope value for your second function.\n';
prompt4='Enter your y-intercept value for your second function.\n';
if (isempty (m1))
m1=input(prompt1);
elseif (isempty(b1))
b1 = input(prompt2);
elseif (isempty(m2))
m2 = input(prompt3);
elseif (isempty(b2))
b2 = input(prompt4);
end
infinite = 'These lines have infinitely many solutions.\n';
parallel = 'These lines have no solutions and are parallel.\n';
intersect = 'These lines intersect.\n';
if(m1==m2)&&(b1==b2)
fprintf (infinite)
x=linspace(-10,10,21);
plot(x,m1*x+b1,x,m2*x+b2)
elseif(m1==m2)&&(b1~=b2)
fprintf (parallel)
x=linspace(-10,10,21);
plot(x,m1*x+b1,x,m2*x+b2)
elseif (m1~=m2)
fprintf (intersect)
x=linspace(-10,10,21);
syms xi yi
eqn1 = m1*xi + b1 == yi;
eqn2 = m2*xi +b2 == yi;
sol = solve([eqn1, eqn2], [xi, yi]);
xSol = sol.xi;
ySol = sol.yi;
fprintf('The lines intersect at %d , %d.\n',xSol,ySol);
plot(x,m1*x+b1,'r',x,m2*x+b2,'b--', xSol,ySol,'*g')
end
end
This code displays the results like this:
However, I would like the user to choose between entering the function like above or as shown in the picture below.
I can get each of these to work separately but I'm having no success getting both to work within the same function. I hope that I explained it clearly enough.
I would greatly appreciate any assistance or clarification on this issue.

Accepted Answer

Deepak Gupta
Deepak Gupta on 27 Oct 2021
You can use varargin as the input to your function definition i.e.
function[output]= IPF(varargin)
if(nargin ==0)
prompt1='Enter your slope value for your first function.\n';
prompt2='Enter your y-intercept value for your first function.\n';
prompt3='Enter your slope value for your second function.\n';
prompt4='Enter your y-intercept value for your second function.\n';
m1=input(prompt1);
m2=input(prompt2);
m3=input(prompt3);
m4=input(prompt4);
elseif(nargin ==4)
m1=varargin{1};
m2=varargin{2};
m3=varargin{3};
m4=varargin{4};
end
%Rest of your code
end
You can have conditions for cases when user inputs only 1 or 2 or 3 inputs as well.
  5 Comments
Deepak Gupta
Deepak Gupta on 28 Oct 2021
Thanks for the clairification Stephen. I learned something important.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!