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?

 采纳的回答

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 个评论

This works nicely, Thank you. Can someone breakdown how this works. I do not have a good understanding of functions.
please read: about "@(x)..." http://www.mathworks.com/help/techdoc/ref/function_handle.html
and doc arrayfun
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?

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by