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?
0 个评论
采纳的回答
Walter Roberson
2012-1-30
quad(@(x) x.^3 - a.*x - b, -5, 5)
2 个评论
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 Center 和 File Exchange 中查找有关 Function Creation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!