Defining piecewise function in matlab

In summary, a piecewise function in Matlab is a function that is defined by different mathematical expressions for different intervals of the input variable. It can be defined using either the "piecewise" function or the "if-else" statement, and can have multiple intervals with their own expressions or conditions. To plot a piecewise function, the "ezplot" or "fplot" functions can be used. It is possible for a piecewise function in Matlab to have an infinite number of intervals, but this may affect computational time and memory constraints.
  • #1
tmpr
5
0
How would you write piecewise functions in MATLAB that can take vector inputs?

Here's a function that I'm trying to write.
Code:
function y=g(x)
if x==0
    y=1;
else
y=sin(x)./x;
end
If I call g([0,pi/2]), I want it to return [0,2/pi], but what I get instead is [NaN,2/pi]. I'm guessing when I write x==0, MATLAB is comparing the entire input to 0.
 
Physics news on Phys.org
  • #2
Try this to eliminate the problem at x=0.


g = @(x) 1.*(x==0) + (x~=0).*sin(x)./(x+(x==0));
t=[0 pi/2];
g(t)
 

Related to Defining piecewise function in matlab

What is a piecewise function in Matlab?

A piecewise function in Matlab is a function that is defined by different mathematical expressions for different intervals of the input variable. It is commonly used to model complex or non-linear relationships in data.

How do you define a piecewise function in Matlab?

To define a piecewise function in Matlab, you can use the "piecewise" function or the "if-else" statement. The "piecewise" function allows you to specify the different expressions for each interval, while the "if-else" statement allows you to define conditions for each interval.

Can a piecewise function have multiple intervals in Matlab?

Yes, a piecewise function in Matlab can have multiple intervals. Each interval can have its own mathematical expression or condition.

How do you plot a piecewise function in Matlab?

To plot a piecewise function in Matlab, you can use the "ezplot" or "fplot" functions. These functions allow you to specify the function expression and the interval over which it should be plotted.

Can a piecewise function in Matlab have an infinite number of intervals?

Yes, a piecewise function in Matlab can have an infinite number of intervals. However, it is important to consider the computational time and memory constraints when defining a large number of intervals.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
75
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
594
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
Back
Top