ABC-biophi
ABC-biophi

Reputation: 505

declare complicated function in matlab

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.

Upvotes: 1

Views: 517

Answers (2)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

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, inline is 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:

>> f = @(x) 3/2*7.02^2 - 2*18*x.*(1-x/18).*(1-exp(-18./x));    
>> root1 = fzero(f, 14)
root1 = 
    1.440303362822718e+01

>> root2 = fzero(f, 2.5)
root2 = 
     2.365138420421266e+00

>> root3 = fzero(f, 0)    %# (if you're into that kind of perversion)
root3 = 
    0

I found the initial values by randomly testing values from -100:100 and then unique-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).

Upvotes: 0

AGS
AGS

Reputation: 14498

You had some problems with your parenthesis and needed to add single quotes:

>>f=inline('((3/2)*(7.02^2))-(2*18*x*((1-(x/18))))*(1-(exp(-18/x)))')

Upvotes: 1

Related Questions