Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Walter Roberson
Walter Roberson il 30 Gen 2012
quad(@(x) x.^3 - a.*x - b, -5, 5)
  2 Commenti
Daniel
Daniel il 30 Gen 2012
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 il 30 Gen 2012
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by