Search code examples
maxima

definition of multipart equations in maxima


How do I express in maxima an equation defined in two parts?

  f(x) = 4x  for 0 < x <= 1/2
         4-4x for 0.5 < x <= 2

Solution

  • Well, Maxima allows partially-evaluated conditional expressions, i.e., when the condition doesn't evaluate to true or false, the result is a conditional expression (otherwise, the condition is true or false, and you get one branch or the other). E.g.

    f(x) := 
        if x > 0 and x <= 1/2 
            then 4*x 
        elseif x > 1/2 and x <= 2 
            then 4 - 4*x;
    
    f(1.5);
     => -2.0
    
    f(a);
     => if a > 0 and a <= 1/2 then 4*a elseif a > 1/2 and a <= 2 then 4-4*a
    
    assume (a > 1 and a < 2) $
    f(a);
     => 4-4*a
    
    plot2d (f(x), [x, -1, 3]);
        => (makes a nice plot)
    

    Notes. (1) if all conditions are false (e.g., x = 2.5) then the result of the conditional expression is false. plot2d just ignores any nonnumeric values, but if you are using f(x) in some way you have to take that into account. (2) Maxima doesn't know much about formally manipulating conditional expressions. Maybe you can say more about what you are trying to achieve here.