plot function f(x)
Show older comments
Referring to figure below, plot the function f(x) in the domain −2 ≤ x ≤ 5 using Method 1 and Method 2.
a) Method 1: write a program in a script file using conditional statements and loops
b) Method 2: create a user-defined function for f(x) and then use the function in a script file to make the plot.

Answers (2)
darova
on 21 May 2020
0 votes
Here are some suggestions for method 1

6 Comments
xavier
on 21 May 2020
darova
on 21 May 2020
You made some mistakes, look

Walter Roberson
on 21 May 2020
your else are wrong. not x(k)<=-1 and instead x(k)>-1
darova
on 21 May 2020
YOu can simply your if...else conditions
x = -2:0.1:5; % note the dot '0.1', not '0,2'
for k = 1:length(x)
if x(k) < -1
% condition 1
elseif x(k) < 1
% condition 2
elseif x(k) < 3
% condition 3
elseif x(k) < 4
% condition 4
else
% condition 5
end
end
plot(x,y)
Sasika
on 28 Jun 2023
0 votes
f(x)=x^2+5*x-6 -8<x<3
1 Comment
Walter Roberson
on 28 Jun 2023
That code will fail for non-scalar x, or for x numeric that is not either positive integer or else logical true or false. And it likely gives u desirable answers in most cases.
Please do some testing of your code before posting your solutions as Answer to a Question.
Categories
Find more on 2-D and 3-D Plots 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!

