フィルターのクリア

Plotting a piecewise function using either for or while loops

1 回表示 (過去 30 日間)
Ben
Ben 2012 年 5 月 24 日
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 Center および File ExchangeDirected Graphs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by