Having Trouble with My Input Argument!

1 view (last 30 days)
function output=myfun(x)
x=3
if x<-2
output=-x^2;
elseif (-2<=x)&&(x<0)
output=-2*x;
elseif (0<=x)&& (x<=2)
output=2*x;
elseif x>2
output=x^2;
end
  8 Comments
Image Analyst
Image Analyst on 2 Jul 2014
Even though it doesn't explicitly tell you to pass in anything, it's pretty much implicit that if you want to test your function to see if it works, you must pass in something, like Azzi's showing you, unless you just want to turn in your assignment untested (not a wise idea).
David
David on 2 Jul 2014
I edited my question and code.
If i do the code as shown, i get the exact answer i'm looking for when i run it. My problem is that i want my input to deal with all possible x values, how exactly do i do this? I shouldn't have to go to my function and edit my x value each time i want to test it, right?

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 2 Jul 2014
Edited: Azzi Abdelmalek on 2 Jul 2014
function output=myfun(x)
if x<-2
output=-x^2;
elseif -2<=x && x<0
output=-2*x;
elseif 0<=x && x<=2
output=2*x;
elseif x>2
output=x^2;
end
  4 Comments
David
David on 2 Jul 2014
I edited my question and code.
If i do the code as shown, i get the exact answer i'm looking for when i run it. My problem is that i want my input to deal with all possible x values, how exactly do i do this? I shouldn't have to go to my function and edit my x value each time i want to test it, right?
Azzi Abdelmalek
Azzi Abdelmalek on 2 Jul 2014
Edited: Azzi Abdelmalek on 2 Jul 2014
What x=3 is doing inside your function, you have first, to understand how functions work. When you call your function
y=myfun(3),
the function will assign 3 to the argument x, and run your code for this value, if you want to test another value, just write
y=myfun(-1)

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!