how to integrate over array boundaries

Hi Guys ,
I want to write a code which includes an array like x=[1 2 3;4 5 6] and
my function is
f=@(y) (y-x).^2
and
Q= integral ( f,0,x);
. I couldn't be successful about it and I need help how can I take an integral over array boundries like this.
Thanks.

답변 (3개)

Roger Stafford
Roger Stafford 2015년 3월 19일

1 개 추천

If you consult the website:
http://www.mathworks.com/help/matlab/ref/integral.html
you will find that 'integral' does not accept arrays for its integration limits. You must use scalar (individual numbers) quantities for these limits. This means of course that you will have to use for-loop constructs to compute Q in a corresponding matrix for each of the separate values in x.
Mike Hosea
Mike Hosea 2015년 3월 19일
편집: Mike Hosea 2015년 3월 19일

1 개 추천

Two steps to set it up. First make it work with a scalar x:
>> Qscalarx = @(x)integral(@(y)(y - x).^2,0,x);
Now vectorize it with ARRAYFUN:
>> Q = @(x)arrayfun(Qscalarx,x);
Now use it.
>> Q([1 2 3;4 5 6])
ans =
0.33333 2.6667 9
21.333 41.667 72

댓글 수: 4

Dear Mike , Thank you for your answer I tried it like below ,
x=in(:,3:4);
Qscalarx = @(x)integral(@(w) kr*(in(:,3:4)-w)*(k*l*vi/(wr*ccons))*(((1+p*l)*vi/ccons).^(k-1))*2.71.^((-((1+p*l)*vi/ccons)).^k),0,x);
Q = @(x)arrayfun(Qscalarx,x);
Q(x);
But it didn't work. Am I missing smth ?
Mike Hosea
Mike Hosea 2015년 3월 20일
편집: Mike Hosea 2015년 3월 20일
Well, you put in(:,3:4) in your function instead of x. It's crucial that x be a scalar in the Qscalarx function. Everything needs to be a scalar except the variable of integration (w), and in(:,3:4) certainly isn't.
Do you really have a function like that is linear in w but has all those other terms in it to define constants that you want to re-evaluate every time a linear integrand is evaluated? Or are some of these constants really functions of w and you just haven't defined or referenced them as such?
Actually I know all constants except 'p', P is a function WRT w.
p=@(w)w/wr;
other letters are all known constants.
Then you need to put p(w) in there everywhere you have just p, and each term involving w anywhere must use an element-wise operator rather than a matrix operator when it connects with other terms depending on w.
Qscalarx = @(x)integral(@(w)k1*(x-w)*(k*l*vi/(wr*ccons)).*(((1+p(w)*l)*vi/ccons).^(k-1)).*2.71.^((-((1+p(w)*l)*vi/ccons)).^k),0,x);
I'd probably define a couple of constants here.
c1 = kr*(k*l*vi/(wr*ccons));
c2 = vi/ccons;
Qscalarx = @(x)integral(@(w)c1*(x-w).*(((1+p(w)*l)*c2).^(k-1)).*2.71.^((-((1+p(w)*l)*c2)).^k),0,x);

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

Salah Djerouni
Salah Djerouni 2020년 2월 8일

0 개 추천

Professor Mike Hosea,
if you possible to help me ,i want to calculate intensity arrias with the equation below in matlab sure :
Capture.PNG
with
a(t) is vector of acceleration
and
t : is vector of time

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

태그

질문:

2015년 3월 19일

답변:

2020년 2월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by