### Tags: afunction, arevectors, command, constants, integrate, matlab, programming, quad, single, variable, vectors

# How to use quad with vectors

On Programmer » Matlab

2,782 words with 2 Comments; publish: Wed, 07 May 2008 19:30:00 GMT; (20050.78, « »)

Hi,

I am having issues using the command quad. I wish to integrate a

function of a single variable, but where some of the constants are

vectors, i.e.

f(x) = a.*x

where x is the variable and 'a' is a vector of constants. My simple

code is:

***

a=linspace(1,4,20);

quad(.matlab.todaysummary.com.(x) a.*x,0,1)

***

but I obtain an error:

? Error using ==> times

Matrix dimensions must agree.

Error in ==> quad at 62

y = f(x, varargin{:});

The problems lies with 'a' being a vector, since if I put a=2, the

code runs fine. I could easily put the code in a loop, so that quad

is called 20 times, but is there a better way to do this? and why

doesnt quad work with vectors?

Thank you

David

*http://matlab.todaysummary.com/q_matlab_29049.html*

All Comments

Leave a comment...

- 2 Comments
- David Duchesne wrote:
>

> Hi,

> I am having issues using the command quad. I wish to integrate a

> function of a single variable, but where some of the constants are

> vectors, i.e.

> f(x) = a.*x

> where x is the variable and 'a' is a vector of constants. My simple

> code is:

> ***

> a=linspace(1,4,20);

> quad(.matlab.todaysummary.com.(x) a.*x,0,1)

> ***

> but I obtain an error:

> ? Error using ==> times

> Matrix dimensions must agree.

> Error in ==> quad at 62

> y = f(x, varargin{:});

> The problems lies with 'a' being a vector, since if I put a=2, the

> code runs fine. I could easily put the code in a loop, so that quad

> is called 20 times, but is there a better way to do this? and why

> doesnt quad work with vectors?

Because quad is not designed to work with

vector valued functions. It is designed to

work only with scalar functions of one

variable.

As you have defined f(x), it returns a

vector. Have you looked at quadv? It is

designed to solve your problem.

HTH,

John D'Errico

#1; Wed, 07 May 2008 19:31:00 GMT

- David Duchesne wrote:
- Dear John,
thanks, that was exactly what I was looking for. What is odd is how I

had never heard of this function. The help files for quad and quadl

dont make any reference to this command, although quadv does

reference quad and quadl. Anyhow, thanks again,

David

John D'Errico wrote:

>

> David Duchesne wrote:

a

> are

> simple

> the

> quad

why

> Because quad is not designed to work with

> vector valued functions. It is designed to

> work only with scalar functions of one

> variable.

> As you have defined f(x), it returns a

> vector. Have you looked at quadv? It is

> designed to solve your problem.

> HTH,

> John D'Errico

#2; Wed, 07 May 2008 19:32:00 GMT

- Dear John,