Integral operator in a for loop

Hi folks! I am trying to calculate the integral of a function while it is in a loop,but MATLAB gives error. How can I change "v" each time? this is the code
f=@(y) sqrt(y)/(1+exp(y/v))
for v=-5:0.1:5
q(i)=integral(f,0,inf);
end

6 个评论

If we want to calculate double integral in a loop then what will be the modification in code below. If any body know the answer please help me. Any suggestions will be appreciated. Thanks in advance.
v= 1:1:5;
L= length(v)
for i=1:L
w=v(i)
fun= @(x,y) w.*x.*exp(-x.*y./w);
c(i)= @(y) integrate(@(x)fun(x,y), 0,Inf);
fun1= @(y) y+c(x)
c1(i)= integrate(fun1, 0,1)
end
diplay(c1())
Use integral instead of integrate -- just a small mistake in the name of the function.
so, I need to create a loop that calculates integrals at each point. Say I have the following function handle
D = @(x,y) x.*(Z(x,y).^2);
from that function, I need to calculate a double integral and create the following loop
int1 = @(y) integral(@(x)D(x,y),x1,x2,'ArrayValued',true);
int2 = integral(@(y)int1,y1,y2,'ArrayValued',true);
c1 = int2./g % g is a scalar number
int11 = @(y) integral(@(x)D(x,y),x2,x3,'ArrayValued',true);
int22 = integral(@(y)int11,y1,y2,'ArrayValued',true);
c11 = int22./g % g is a scalar number
int111 = @(y) integral(@(x)D(x,y),x3,x4,'ArrayValued',true);
int222 = integral(@(y)int111,y1,y2,'ArrayValued',true);
c111 = int222./g % g is a scalar number
.
.
.
i'm trying to do like this
x = 1:0.1:5
for i = 1:length(x)
D = @(x,y) x.*(Z(x,y).^2);
int1(i) = @(y) integral(@(x)D(x,y),x(i),x(i)+0.5,'ArrayValued',true);
int2(i) = integral(@(y)int1(i),y1,y2,'ArrayValued',true);
c = int2(i)./g
end
I need to plot a graph of c versus x
Could you confirm that Z is a function?
It is confusing that you are using the same variable name x for your original vector that your for i is for, and also being the parameter name for integrate() which will be a vector reflecting local x conditions within the bounds. With similar concerns for y
It gets confusing when the same question is posted in multiple places! https://www.mathworks.com/matlabcentral/answers/509422-double-integration-with-loop
I thought that this way it would be easier to understand. I'm sorry for this
dF = @(z1,r) 2.*pi.*v.*ro.*(Brt(z1,r).^2).*r; % v, ro they are scalar numbers
p1 = @(r) integral(@(z1) dF(z1,r),z0,z0+e,'ArrayValued',true);% e is also a scalar
F = integral(@(r) p1,ri,re,'ArrayValued',true);
c = F./v
that's my role in response to z0. what I need is for it to be calculated with the z0 varying
z0 = 0:0.001:0.03
for each variation of z0, I will have a new p1, a new F and, consequently, a new c. I want to plot a graph of z0 versus c

请先登录,再进行评论。

回答(2 个)

v = -5:.1:5;
f = @(y)sqrt(y)./(1+exp(y./v));
q = integral(f,0,inf,'ArrayValued',true);
vdata=-5:0.1:5;
n=length(vdata);
q=zeros(1,n);
for i=1:n
v=vdata(i);
f=@(y) sqrt(y)/(1+exp(y/v));
q(i)=integral(f,0,inf);
end

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by