How to rewrite a non-linear function so that f(x) = 0?

7 views (last 30 days)
Hello! This is my first question on these forums, so please forgive me if I do anything wrong here :). What I wonder about is how to make MATLAB rewrite a general, non-linear function. Say that I input the function "3cos(x) = x^2" or "e^x = 2x". Is there any function for this? What I want to do with this new, rewritten function is to use it to find the point of zero. This I plan to do with a function of my own, instead of using fzero. Thank you all in advance!

Accepted Answer

Walter Roberson
Walter Roberson on 16 Dec 2011
You would have to input the equation as a string. Search for the '=' in the string, insert a '(' immediately after that and a ')' at the end, and convert the '=' to '-'. The result will be an expression with an implicit "= 0"
After that you have to figure out how to do calculations based upon a string. For that, please see http://www.mathworks.com/matlabcentral/answers/22330-can-you-have-the-user-input-an-equation-in-matlab

More Answers (4)

Honglei Chen
Honglei Chen on 16 Dec 2011
Isn't it just
3*cos(x)-x^2
and
exp(x)-2*x
If you have to, you can define function f1(x) for the left side and f2(x) for the other side and then use f1(x)-f2(x). But IMHO it really doesn't save you anything.

Christian Skoog
Christian Skoog on 16 Dec 2011
I want MATLAB to do exact that calculation, yes. How do I write the code so that it does that?

Sean de Wolski
Sean de Wolski on 16 Dec 2011
If you have the Symbolic Math Toolbox, you can just use solve()
solve('exp(x)=2*x')
solve('3*cos(x)=x^2')
doc solve
Welcome to MATLAB Answers!
MORE
syms x
f1 = x^2-3
f2 = cos(x*exp(x))
simple(f2-f1)

Christian Skoog
Christian Skoog on 16 Dec 2011
No, I don't want the solution to the calculation. I want MATLAB to be able to rewrite the calculation. I want MATLAB to be able to transform 3*cos(x)=x^2 to 3*cos(x)-x^2=0, and e^x=2*x to e^x-2*x=0. The user would call a function that calculates the point of zero, by inputting a non-linear function and a starting guess. For this I need to be able to rewrite a general non-linear function to one with only 0 to the right of the "equals to" or = sign. How would I go around doing this? "...you can define function f1(x) for the left side and f2(x) for the other side and then use f1(x)-f2(x)" This sounds like a quite good idea. How would I do this when the function would be input for a function within MATLAB?

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!