1

I have been working on solving some equation in a more complicated context. However, I want to illustrate my question through the following simple example.

Consider the following two functions:

function y=f1(x)
    y=1-x;
end

function y=f2(x)
    if x<0
        y=0;
    else
        y=x;
    end
end

I want to solve the following equation: f1(x)=f2(x). The code I used is:

syms x;
x=solve(f1(x)-f2(x));

And I got the following error:

??? Error using ==> sym.sym>notimplemented at 2621
Function 'lt' is not implemented for MuPAD symbolic objects.

Error in ==> sym.sym>sym.lt at 812
            notimplemented('lt');

Error in ==> f2 at 3
if x<0

I know the error is because x is a symbolic variable and therefore I could not compare x with 0 in the piecewise function f2(x).

Is there a way to fix this and solve the equation?

horchler
  • 18,384
  • 4
  • 37
  • 73
Willowzju
  • 13
  • 4
  • What `version` of Matlab are you using? – horchler Oct 31 '14 at 19:58
  • The ability to use "<" to create a symbolic relation was introduced in release R2012a. – Alex Oct 31 '14 at 20:07
  • There are ways around this, but the Symbolic toolbox has changes so much that solution can depend heavily in version. – horchler Oct 31 '14 at 20:12
  • Thanks for quick response. the version I am using now is 7.11.0 (R2010b). – Willowzju Oct 31 '14 at 20:13
  • The ability to use "<" to create a symbolic relation was introduced in release R2012a. I don't know of ways around this to solve it strictly symbolically. – Alex Oct 31 '14 at 20:19
  • I just tried the same code in version R2013a. However, similar error shows up: " Conversion to logical from sym is not possible. Error in f2 (line 3) if x<0 " – Willowzju Oct 31 '14 at 20:21
  • Have you tried any of these options? http://www.mathworks.com/help/symbolic/mupad_ref/solve.html – Alex Oct 31 '14 at 20:36
  • Thanks so much! "heaviside(x)" works well in this simple example. But "isAlways(sym(x<0))" doesn't work in either the old or new version MATLAB. I will try "heaviside(x)" in a more complex problem and let you know if it doesn't go through. – Willowzju Oct 31 '14 at 21:06

1 Answers1

1

First, make sure symbolic math is even the appropriate solution method for your problem. In many cases it isn't. Look at fzero and fsolve amongst many others. A symbolic method is only needed if, for example, you want a formula or if you need to ensure precision.

In such an old version of Matlab, you may want to break up your piecewise function into separate continuous functions and solve them separately:

syms x;
s1 = solve(1-x^2,x) % For x >= 0
s2 = solve(1-x,x)   % For x < 0

Then you can either manually examine or numerically compare the outputs to determine if any or all of the solutions are valid for the chosen regime – something like this:

s = [s1(double(s1) >= 0);s2(double(s2) < 0)]

You can also take advantage of the heaviside function, which is available in much older versions.

syms x;
f1 = 1-x;
f2 = x*heaviside(x);
s = solve(f1-f2,x)

Yes, the Heaviside function is 0.5 at zero – this gives it the appropriate mathematical properties. You can shift it to compare values other than zero. This is a standard technique.

In Matlab R2012a+, you can take advantage of assumptions in addition to the normal relational operators. To add to @AlexB's comment, you should convert the output of any logical comparison to symbolic before using isAlways:

isAlways(sym(x<0))

In your case, x is obviously not "always" on one side or the other of zero, but you may still find this useful in other cases.

If you want to get deep into Matlab's symbolic math, you can create piecewise functions using MuPAD, which are accessible from Matlab – e.g., see my example here.

Community
  • 1
  • 1
horchler
  • 18,384
  • 4
  • 37
  • 73