I'm trying to have a function receive input in 2 different ways.
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Mathematics in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

