简体   繁体   中英

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.

您的圆括号存在一些问题,需要添加单引号:

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

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, aka 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM