descontinuous initial data [closed]
I have a discontinuous function like
f(x)=0.5(exp(-80x^2)+1) if -0.3<x<-0.1
开发者_如何学Python
and
f(x)=0.5exp(-80x^2) otherwise,
with the domain 0 ≤ x ≤ 1.
How to define it as inline function in MatLab?
Since the OP poses the domain as 0 <= x <= 1, then the answer is simply to use the positive branch! There is no need to worry about the value when x is negative.
Next, I would suggest that you do NOT use an inline function at all. Inline functions are slow. Use them only if your matlab release is so old that you cannot define a function handle. So then
f = @(x) 0.5*exp(-80*x.^2);
If you must define an inline function, then I'd really suggest you get a newer version of matlab. If you still refuse to enter the current century, then do this:
f = inline('0.5*exp(-80*x.^2)','x');
There is one other possibility, and that is you have also screwed up the domain of the function. If the domain of the function is not strictly 0 <= x <= 1 so that negative values can occur, then we might need to worry about the discontinuous nature of the function. Here you can use something like piecewise_eval, as posted on the MATLAB Central File Exchange. This tool allows you to build and evaluate piecewise functions, and you could build it into either an inline function or an anonymous function/function handle as desired. Thus, this expression will build a function handle for your purpose:
f = @(x) piecewise_eval(x,[-.3, -.1], ...
{@(x) 0.5*exp(-80*x.^2), ...
@(x) 0.5*(exp(-80*x.^2) + 1), ...
@(x) 0.5*exp(-80*x.^2)});
One other thing to beware of is what happens at the break points themselves. The piecewise_eval function assumes that a point will lie inside half open intervals, such as:
-0.3 <= x < -0.1
for the middle branch of your function. This is fairly standard in most such definitions of piecewise functions.
In Octave (probably MATLAB too), a logical expression evaluates to 0 and 1. Therefore,
y = inline('0.5*(exp(-80*x^2) + (-0.3 < x && x < -0.1))', 'x')
精彩评论