MATLAB Answers

0

Integral operator in a for loop

Asked by amin rasoulof on 27 Mar 2013
Latest activity Commented on by Walter Roberson
on 15 Nov 2017
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

  2 Comments

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.

Sign in to comment.

2 Answers

Answer by Andrei Bobrov
on 27 Mar 2013
Edited by Andrei Bobrov
on 27 Mar 2013

v = -5:.1:5;
f = @(y)sqrt(y)./(1+exp(y./v));
q = integral(f,0,inf,'ArrayValued',true);

  0 Comments

Sign in to comment.


Matt J
Answer by Matt J
on 27 Mar 2013

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

  0 Comments

Sign in to comment.