How do I integrate a very simple function with specified inputs?

1 次查看(过去 30 天)
What I want to do is very simple, and so I am very frustrated. I simply want to integrate a function of multiple variables, i.e. a function with inputs (arrays) to be specified, say, a function involving a variable to be integrated over, x, and inputs, like a and b::
quad('a.*x - b',0,10)
I continue receiving this error message:
??? Error using ==> inline.subsref at 14
Not enough inputs to inline function.
My actual code is below
global a b
a = 1; b = 2;
integrand = inline('(x.^3-a*x-b)','a','b');
quad(integrand,-5,5)
I've tried everything I can think of, and making it as simple as possible, as you can tell. What am I doing wrong?

采纳的回答

Walter Roberson
Walter Roberson 2012-1-30
quad(@(x) x.^3 - a.*x - b, -5, 5)
  2 个评论
Daniel
Daniel 2012-1-30
Thanks for your post; I'll try that. I've been reading through _Getting Started with MATLAB 7_ by Rudra Pratap, and reading through MATLAB documentation (e.g. 'help quad'), and I still have absolutely no clue what this @ business is all about, or what a "function handle" is or does or is used for. How can I learn more and understand it?
Walter Roberson
Walter Roberson 2012-1-30
http://www.mathworks.com/help/techdoc/ref/function_handle.html
http://www.mathworks.com/help/techdoc/matlab_prog/f4-70115.html
Languages such as C would refer to a function handle as a "pointer to a function" (except it is more powerful than that.)
You can use a function handle nearly any place that you would use the name of a real function. You cannot take @ of a function handle, though ;-) And also, when a real function is named in an expression with no following arguments, then the function would be executed with no arguments, whereas when a function handle is named in an expression with no following arguments, then the function designated is not invoked.
a = rand; %rand is invoked with no arguments
myrand = @rand; %function handle is created
a = myrand; %function is not invoked -- function handle is copied
a = rand(3,3); %rand is invoked with (3,3) as argument
a = myrand(3,3); %myrand is invoked with (3,3) as argument

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by