Having Trouble with My Input Argument!
1 view (last 30 days)
Show older comments
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
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).
Accepted Answer
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
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)
More Answers (0)
See Also
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!