Need help with plotting a complicated for loop
Show older comments
Create a function called f that receives an input vector and satisfies the following criteria:
For values of x > 2, f(x) = x^2
For values of x ≤ 2, f(x) = 2x
Plot your results for values of x from –3 to 5. The plot should have a title, and both the x and y axis should be labeled.
I've got my function written and it works, but when I run it, it only plots values for when f(x)=x^2. I know I have a small mistake somewhere, and I'd appreciate if someone could help me out. Here's what I've got so far:
function y=f(x)
for i=x
if i>2
y=x.^2;
elseif i<=2
y=2*x;
end
end
xlabel 'x'
ylabel 'f(x)'
title('Unit Example 4, Problem 6')
plot(x,y,'k*')
end
Accepted Answer
More Answers (0)
Categories
Find more on Programming 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!