How to define a function consisting of multiple parts (i.e different f:n at different times) in Matlab using a single equation?

168 ビュー (過去 30 日間)
Example
f(x)
= x^2 for 0<x<1;
= x^3 for 1<x<2;
= ......and so on.
Please help me with this.

採用された回答

Sally Al Khamees
Sally Al Khamees 2016 年 12 月 23 日
編集済み: Sally Al Khamees 2017 年 2 月 21 日
If you have R2016b and the Symbolic Math Toolbox installed, you can just use the piecewise function:
Here is an example:
syms y(x);
y(x) = piecewise(1<x <2, x^3, 2 <= x <= 3, x^2, 1)
fplot(y)
%You can evaluate it at one point. Example when x = 2
y(2)
%You can evaluate a vector. Example
v = linspace(1,4,4)
y(v)
  1 件のコメント
Samuel Silas Ng'habi
Samuel Silas Ng'habi 2023 年 4 月 27 日
What if the value and the conditions are expressed as nth -term. How will you plot the function See pictures attached

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 4 月 22 日
You can define it symbolically using MuPAD's "piecewise" construct.
In some cases you can define it numerically using logical constructs such as
(x > 1 & x < 2) .* x.^3 + (x > 0 & x < 1) .* x^2
This will not work properly for locations that generate NaN or infinity when evaluated for any part. For example, if f(x) = 1 for x = 0, and f(x) = 1/x for other x, then you cannot use
(x == 0) .* 1 + (x ~= 0) .* 1./x
because the second part will generate NaN when evaluated for x(K) = 0, and the NaN multiplied by the 0 of (x(K) ~= 0) will still be NaN instead of vanishing to 0 as it does for finite values. Similarily, 0 * inf is NaN rather than 0.

John BG
John BG 2016 年 12 月 23 日
y=[1:0.001:2].^3
  4 件のコメント
Stephen23
Stephen23 2017 年 2 月 22 日
@John BG: how could this be used in a function of x (as the question requests), e.g.:
fun = @(x) ???
Note that both Sally Al Khamees' and Walter Roberson's answers provide this.
Walter Roberson
Walter Roberson 2017 年 2 月 22 日
Consider, for example, if the task is to find the point at which the function equals 3.5,
x0 = 2 * rand(); %range is 0 to 2
fzero(@(x) f(x) - 3.5, x0)
Using a fixed dx is not going to be able to handle this task -- not unless dx = eps(realmin), so that you are testing all 2^62 representable numbers between 0 and 2.
You could, of course, write code that assumes that the input is a scalar:
function y = f(x)
y = 0;
if x > 0 & x < 1
y = x.^2;
elseif x > 1 & x < 2
y = x.^3;
end
end
and you could loop that code for non-scalar x.
You can use logical indexing:
function y = f(x);
y = zeros(size(x));
mask = x > 0 & x < 1;
y(mask) = x(mask).^2;
mask = x > 1 & x < 2;
y(mask) = x(mask).^3;
end
You can define it with an anonymous function,
f = @(x) (x > 1 & x < 2) .* x.^3 + (x > 0 & x < 1) .* x^2;
You can look at the pattern and predict
f = @(x) (x ~= ceil(x)) .* x.^(1 + ceil(x));
And all of those versions are functions that could be used as functions over arbitrary domains such as for fzero() purposes.
But using a fixed dx is not an approach that can be used for this kind of common application.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by