0

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); 
India Slaver
  • 33
  • 10
  • Hey, you changed your question! It looks like you are having trouble understanding the way *sym* works. You should spend some time, and if you can't figure it out ask another question. – Mikhail Dec 02 '14 at 11:07
  • @Mikhail No, I just edited the question and added the later part. It still gives me an error when I specify the check-point. – India Slaver Dec 02 '14 at 11:08
  • Note that you can also solve symbolic equations with Matlab. Make sure to check `doc solve` – Dennis Jaheruddin Dec 02 '14 at 13:58
  • @DennisJaheruddin Matlab hangs when I use this function. My original equation is too lengthy. :( – India Slaver Dec 02 '14 at 14:34

2 Answers2

0

You need to specify a guess, x0 point

fun = @sin; % function
x0 = 3; % initial point
x = fzero(fun,x0)
Mikhail
  • 7,749
  • 11
  • 62
  • 136
0

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)
am304
  • 13,758
  • 2
  • 22
  • 40
  • Ok, but it's still not clear. Could you please tell me how would I add 2 functions. Actually my problem is not this problem. Let's suppose, I have. `fun1 = @(x)sin(cosh(x))` and `fun2 = @(x)cos(cosh(x))`. Now how can I add these two functions like `fun3 = fun1 + fun2` and solve `fun3` using fzero. Please help – India Slaver Dec 02 '14 at 11:20
  • I answered your question. If you have a different question, please ask it in a separate thread, otherwise it becomes confusing. And make it clear and explicit: I don't really understand what you mean. You can only solve one function/equation at a time. Your question doesn't make sense. – am304 Dec 02 '14 at 11:22