defining piecewise function in matlab

by tmpr
Tags: defining, function, matlab, piecewise
tmpr is offline
Oct27-10, 12:46 AM
P: 5
How would you write piecewise functions in matlab that can take vector inputs?

Here's a function that I'm trying to write.
function y=g(x)
if x==0
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.
Phys.Org News Partner Science news on
Going nuts? Turkey looks to pistachios to heat new eco-city
Space-tested fluid flow concept advances infectious disease diagnoses
SpaceX launches supplies to space station (Update)
matematikawan is offline
Nov3-10, 12:30 AM
P: 330
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];

Register to reply

Related Discussions
MATLAB - piecewise defined Fn as periodic Engineering, Comp Sci, & Technology Homework 1
Defining implicit function given a parametric function Calculus & Beyond Homework 1
Finding constants in a piecewise function that allow the function to be differentiabl Calculus & Beyond Homework 5
We need help on piecewise function.... General Math 11
Piecewise function Calculus 1