Plotting a piecewise function using either for or while loops

7 次查看(过去 30 天)
I want to make the following piecewise function:
f(x) = 5x for 0<x<3 = 2x^2 for 3<x<5 and = 32*ln(4/x)+16 for 5<x<7
and I need to make an M-script that evaluates for values of x starting at 0, and increasing in increments of 0.2 until x = 7, as well as showing a plot of the function against the x values.
Scripts so far:
for i = 3:0.2:5
y = 2*(i^3)
x = [3:0.2:5]
plot (x, y)
end
The first function calculates fine for 0<x<3, but I’m wondering why this code doesn’t graph the first part of the function, and how I could get it to graph. Also, how do I implement the other functions in as well? Do I connect more loops?
Thanks for your assistance. I'm relatively new to MATlab, so I don't understand advanced codes - so if possible please keep it simple. =)

采纳的回答

Walter Roberson
Walter Roberson 2012-5-24
x = 0:.2:7;
y = 0 * x;
range = a < x & x < b;
y(range) = F1(x(range));
for appropriate a and b and F1(x) that applies over a to b (exclusive)
Note: there are some values at which your piecewise function is not defined. Such as x == 3 exactly.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by