Plotting a piecewise function using either for or while loops

2 Ansichten (letzte 30 Tage)
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. =)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Mai 2012
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.

Weitere Antworten (0)

Kategorien

Mehr zu Networks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by