Search code examples
matlabequationnonlinear-functions

Not able to use `fzero()` function in Matlab


I am new to Matlab. I am trying to solve a non-linear equation using this inbuilt Matlab function called fzero() but it's not giving me the results.

The main file goes like

A = 5;
B = 6;
C = 10;

eq = equation (A, B, C);

fzero(@(x)eq);

The other function file is:

function eq = equation (A, B, C)

syms x;
eq = A*x.^2 + B*x + C*(asinh(x)) ;

When I run this code, I get the following error:

Error using fzero (line 118)
The input should be either a structure with valid fields or at least two arguments to
FZERO.

Error in main (line 7)
fzero(@(x)eq); 

Could someone help me with this?

EDIT: WHen I specify the check point as 0, it returns me the following error.

Undefined function 'isfinite' for input arguments of type 'sym'.

Error in fzero (line 308)
    elseif ~isfinite(fx) || ~isreal(fx)

Error in main (line 7)
fzero(@(x)eq, 0); 

Solution

  • There are several mistakes in your code. For a start, fzero is for finding numerical roots of a non-linear equation, it is not for symbolic computations (check the documentation), so get rid of syms x. The correct way to call fzero in your case is a as follows:

    A = 5;
    B = 6;
    C = 10;
    eq = @(x) A*x^2 + B*x + C*(asinh(x));
    x0 = 0; % or whatever starting point you want to specify
    x = fzero(eq,x0)