Integration using a vector as function parameter

조회 수: 2 (최근 30일)
Paul Mitchell
Paul Mitchell 2012년 5월 16일
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

채택된 답변

Mike Hosea
Mike Hosea 2012년 5월 16일
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
Paul Mitchell
Paul Mitchell 2012년 5월 16일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by