How to function a sum

Hello. I need to create a function, say Req, that is made by summing a function defined by:
function R = resist(T)
R = rand_R0(100,1000).*exp((rand_T0(12,17)./T).^(1/4));
and then
Req = 1./(1./resist(T)+1./resist(T)+...)
How can I do this recursively? I need to sum 1000 terms. Actually I need to plot Req versus T.
Thank you in advance.

댓글 수: 9

Walter Roberson
Walter Roberson 2013년 2월 23일
For sort, let us write resist(T) as x. Then it looks like your Req equation is
1 / (1/x + 1/x + 1/x + ...)
Is that correct? Or did you intend
1 / (1/x1 + 1/x2 + 1/x3 + ... )
or did you intend
1 / (1 / (x + 1 / (x + 1 / (x + 1 / (x + ...
?
Andre Luis
Andre Luis 2013년 2월 24일
The correct expression is the first one.
1 / (1/x + 1/x + 1/x + ...)
Walter Roberson
Walter Roberson 2013년 2월 24일
Why bother doing it recursively? If all of the resist(T) are the same, adding N of 1/resist(T) will give N/resist(T) and then 1 / that will give resist(T) / N, with no calculation needed.
Andre Luis
Andre Luis 2013년 2월 24일
Oh, I'm sorry! I made a mistake. Functions resist(T) are not the same. Functions rand_R0 and rand_T0 make then different. So Req is made by different terms: Req = 1/(1/x1+1/x2+...).
Walter Roberson
Walter Roberson 2013년 2월 24일
Is each of the calls to resist returning one value or a vector or an array?
If it is returning one value, could rand_R0 and rand_T0 be extended to make them vectorized, to return a number of values at once?
Andre Luis
Andre Luis 2013년 2월 24일
Functions rand_R0(a,b) and rand_T0(c,d) return one value between (a,b) and (c,d). Yes, it could be extended to return a number of values. But how could this help me?
Walter Roberson
Walter Roberson 2013년 2월 24일
If you had it return the 1000 then you would just vectorize everything.
I have not figured out yet why you think recursion would be a good idea for this code.
Walter, I really appreciate your help. Like I said in the comment below, I'm trying to write a function Req that is made by summing the function R which is function of T (so Req is also function of T). It is a problem of parallel resistor and they depend on T (temperature). My functions are
function R = resist(T)
R = rand_R0(100,1000).*exp((rand_T0(12,17)./T).^(1/4));
and
function R0 = rand_R0(R01,R02)
RR0 = R01 + (R02-R01).*rand(1);
R0 = ceil(RR0);
(the same for T0)
And then, plot Req(T) versus T. Do you have any idea how vectorize it?
Andre Luis
Andre Luis 2013년 2월 25일
Help?

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

답변 (2개)

Muthu Annamalai
Muthu Annamalai 2013년 2월 23일
편집: Muthu Annamalai 2013년 2월 23일

0 개 추천

You can try something like,
function val = Req( fhandle, arg, N )
val = 1;
for itr = N:-1:1 %build from smallest value term upwards to term 1.
val = fhandle(arg,itr) + 1/val;
end
end
While a recursive solution would work most of the times, since MATLAB has a limited stack size, and doesn't convert a tail-recursive call to an iterative solution, you want to write out the iteration explicitly.

댓글 수: 1

Andre Luis
Andre Luis 2013년 2월 24일
Sorry, but what is fhandle? My resist function depends only on T.

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

Youssef  Khmou
Youssef Khmou 2013년 2월 24일

0 개 추천

hi Andre,
are you trying to write a program that sums Parallel resistors , well you did nt define the whole parameters such rand_R0 and rand_T0 ,
You can use one single function with nested Req function in :
try :
%-----------------------------------------------------------
function Req=Sum_resistors(N)
for T=1:N
Req(T) =1./(1./resist(T));
end
T=1:N;
figure, plot(T,Req), title(' Your funcrion : Resistors vs some Physical param')
function R = resist(T)
R = rand(1).*exp((rand(1)./T).^(1/4));
return
%--------------------------------------------------------------------
I adjusted Rand_R0 and Rand_To so as Req returns a scalar .
You said that you want plot Req vs T, summing wil only return a scalar, i suggest to use cumulative summation:
>>R =@(T) rand(1).*exp((rand(1)./T).^(1/4));
>>plot(cumsum(Req(1:1000)))
>>plot(Req(1:1000))

댓글 수: 3

Andre Luis
Andre Luis 2013년 2월 24일
Hi Youssef, thanks for your help.
Sorry, I forgot to say that rand_R0(a,b) and rand_T0(c,d) are functions that return a number between the arguments (a,b) and (c,d).
I tried to use cumsum but I don't know if it gives the result that I'm looking for.
Andre, ok, So you want verify if the serie converges when N->Inf ? i think cumsum will not show you the convergence, lets try a simple prototype :
>>R=50; %50Ohm with identical elements then :
>>Req=R./(1:1000);
>>plot(Req) % It plots directly how the total resistance is when we %increment them 1:N : Decreasing function.
>>figure,plot(cumsum(Req)) % Log like function
which one seems logical to your system ?
Andre Luis
Andre Luis 2013년 2월 24일
Actually, what I'm trying to do is write a function Req that is made by summing the function R which is function of T (so Req is also function of T). Then plot Req versus T.

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

태그

질문:

2013년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by