I wanted to declare a nonlinear complicated function in Matlab, so I wrote this :
>> syms x
>> f=inline((3/2)*(7.02^2))-(2*18*x*((1-(x/18))*(1-(exp(-18/x)))))
but it did not work and it returns this error :
??? Error using ==> inline.inline at 47
Input must be a string.
How can I declare it so that I can use it as a function inside a loop?
I want to find the root of this function numerically, so I first need to declare it, so that I can use it in a loop.
First, you should learn about operator precedence, so you can avoid many of the confusing brackets you have.
Second, as most other people have mentioned here,
inlineis slow and not suited for this purpose. You’re better off using (and leaning how to use properly) anonymous functions, a.k.a. function handles.Third, if you want to find the roots of this function, you’d better use an extensively tested Matlab function dedicated to that purpose, rather than design & implement your own version:
I found the initial values by randomly testing values from
-100:100and thenunique-ing the outcomes. This is by no means a robust way to find all roots, but I trust you can come up with something better (the problem is fairly straightforward to solve analytically anyway).