definite integral w/o using symbolic variable

I am trying to integrate a function, then store that value, then change a value in the function, integrate again, store that value, and so on.
here is the idea
x=0:10;
x1 = 2;
x2 = 5;
m = 1;
i = 1;
if m < 5
F = inline('m*x');
AUC(i) = quadl(F,x1,x2);
m = m+1;
i = i+1;
end
M=1:4;
plot(M,AUC)
obviously this code does not work. I need to update the line F=inline for every value of m. How do I do this, or is there another way?

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 11월 11일

1 개 추천

m = 1:4;
x1 = 2;
x2 = 5;
AUC = arrayfun(@(i1)quad(@(x)i1*x,x1,x2),m)
loop
n = numel(m);
AUC = zeros(n,1);
for i1 = 1:n
AUC(i1) = quad(@(x)m(i1)*x,x1,x2);
end
ADD
k = 3+4i; % e.g.
q = 6-4i; % e.g.
I = @(x)abs((exp(-((1i*k)/(2*q))*x.^2).^2))/2;
quadgk(I,2,-3*1i)

댓글 수: 4

Randy
Randy 2011년 11월 11일
This works nicely, Thank you. Can someone breakdown how this works. I do not have a good understanding of functions.
Andrei Bobrov
Andrei Bobrov 2011년 11월 11일
please read: about "@(x)..." http://www.mathworks.com/help/techdoc/ref/function_handle.html
and doc arrayfun
Randy
Randy 2011년 11월 11일
here is how I understand what is happening:
m = 1:4;
x1 = 2;
x2 = 5;
AUC = arrayfun(@(i1)quad(@(x)i1*x,x1,x2),m);
% @(i1) gets the value of m
% quad operates on @(x) which is il*x which is m*x
% arrayfun stores that value into AUC as an array
n = numel(m);
AUC = zeros(n,1);
for i1 = 1:n
AUC(i1) = quad(@(x)m(i1)*x,x1,x2);
end
% for il = 1 we get AUC(1) = quad(@(x)m(1)*x,x1,x2)
% quad operates on @(x) which is 1*x
% that value is stored in AUC(1)
Now what I am trying to do is a bit more complicated.
I have a Gaussian function:
E = exp(-((i*k)/(2*q))*x.^2);
I = abs((E.^2))/2;
I want to integrate I from x1 to x2, store that value, change q, then integrate I again and store that value. q is a complex number that is calculated from code that is not shown. Can you give me some advice for how to implement this?
Andrei Bobrov
Andrei Bobrov 2011년 11월 11일
see ADD

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

추가 답변 (0개)

카테고리

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

질문:

2011년 11월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by