3

I want to plot the function f(x,y)=cos(y)sin(x)/x even when x=0 (the function is continuous at such points). I can create a function in Asymptote that plots this surface with triple f(pair t) {return (t.x,t.y,cos(t.y)*sin(t.x)/t.x);} that works fine as long as the plotted points never have t.x=0. However, I do want to plot such points, and I tried to skirt this issue by using an if statement in the definition of the function: when t.x=0, I want the function to just return cos(y).

I tried:

triple f(pair t) {return (t.x,t.y, if(t.x==0) {cos(t.y);} else {cos(t.y)*sin(t.x)/t.x;});}

along with various permutations of existence of ; and placements of {} and (); I also tried

triple f(pair t) {return if(t.x==0) {(t.x,t.y,cos(t.y);} 
else {(t.x,t.y,cos(t.y)*sin(t.x)/t.x);};}

also with various placements of ;, etc. All I get is a vague syntax error message.

How do I combine functions and if statements?

6
triple f(pair t)
{
    if (t.x == 0) { return (t.x, t.y, cos(t.y)); }
    return (t.x, t.y, cos(t.y)*sin(t.x)/t.x);
}

or

triple f(pair t)
{
    return (t.x, t.y, t.x==0 ? cos(t.y) : cos(t.y)*sin(t.x)/t.x);
}
  • This is exactly what I was looking for. I just didn't quite understand the syntax of functions as to properly incorporate the if statement. – GregH May 20 '15 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.