Integration using a vector as function parameter

2 views (last 30 days)
I'm a recent joinee to the MATLAB community and have what I think is fairly simple question but can't seem to find the answer so maybe its less simple than I think.
I have a vector containing a string of data and want to use that data to provide a single parameter in a function for definite integration.
Ultimately I want to write a function that will return a vector that contains the definite integrals of this function using the previous vector as a parameter of the function.
i.e.
N=[1:4];
Crush = @(x,n) x.^n;
M = quad(crush(x,N),0,2);
I know that this doesn't work at least in this form because quad will not accept a function with argument, but hopefully my intention is clear. I was wondering if there is a way to do this without resorting to a complex for/while loop. I suspect that it can be done by nesting functions and I'm just missing something very simple. I'm using x^n as an example for the function.
Thanks for any help,
Paul

Accepted Answer

Mike Hosea
Mike Hosea on 16 May 2012
Not sure of your intention. This is how I interpreted what you wrote:
>> N = 1:4;
>> Crush = @(x,n) x.^n;
>> integral(@(x)Crush(x,N),0,2,'ArrayValued',true)
ans =
2 2.6667 4 6.4
>> quadv(@(x)Crush(x,N),0,2)
ans =
2 2.6667 4 6.4
Use INTEGRAL for 12a and later, QUADV for 11b and earlier releases of MATLAB. -- Mike
  1 Comment
Paul Mitchell
Paul Mitchell on 16 May 2012
Exactly what I was trying to do. Just didn't quite understand how to make the nested function work that way, but that did the trick.
Thank you very much.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!