How do I integrate a very simple function with specified inputs?
조회 수: 12 (최근 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!