Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get several solutions to an arbitrary equation in Matlab?

Suppose I have a function f = @(x) myfun(x);

I can use fzero to get the solution closest to a given x0, but can I get all solutions in a specific area, for instance: -5 < x < 5?

I.e. Is it possible to get a solution similar to the result of roots, but for non-polynomials?

like image 476
Stewie Griffin Avatar asked May 29 '13 09:05

Stewie Griffin


People also ask

Can you solve simultaneous equations in MATLAB?

It is because solving simultaneous equations using Matlab involves the multiplication of the matrix. These equations are simultaneous because one set of x_i must satisfy all the equations of M . Assume that you have the value of A and x to find b , then the equation is easy to solve.

How do you find the solution to an equation in MATLAB?

Description. S = solve( eqn , var ) solves the equation eqn for the variable var . If you do not specify var , the symvar function determines the variable to solve for. For example, solve(x + 1 == 2, x) solves the equation x + 1 = 2 for x.

How do you solve an equation with more than one variable?

First, you can solve for one of the variables, then substitute that value for the variable in the other equation. The other way is to add the equations together (combine), and to do this, sometimes multiplying one of the equation by a positive or negative number is required.


1 Answers

Yes, you can.

There's a nice submission on the file exchange that allows you to do exactly that. It works by approximating your curve by a Chebychev polynomial, and then finding all real roots of that polynomial.

If you want you can use these estimates for the roots as initial values for fzero, but often (at least for smooth and otherwise well-behaved curves) the accuracy demands can already be met by using a higher-order Chebychev approximation.

For your example, using only 18 function evaluations (I have a slightly modified version of the file, but the essence is the same):

>> f = @(A) 17.7*sin(A).*cos(A)+87*sin(A).^2-9.65*cos(A)-47*sin(A);
>> R = FindRealRoots(f, -5,5, 17)
R =
  -3.709993256346244
  -3.345207732130925
  -0.201929737187637
   0.572382702285053
   2.573423209113534
   2.937157987217741

>> R2 = R; 
>> funcCount = 0;
>> for ii = 1:numel(R) 
        [R2(ii), ~,~, output]  = fzero(f,R2(ii)); 
        funcCount = funcCount + output.funcCount;
   end
>> max(abs(R2(:)-R(:)))
ans =
    8.564253235401331e-004
>> funcCount
ans = 
    46

e.g., there is only 8 parts per ten thousand improvement for no less than 46 additional function evaluations.

like image 138
Rody Oldenhuis Avatar answered Oct 05 '22 03:10

Rody Oldenhuis