How to function a sum
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 23 Fév 2013
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
le 24 Fév 2013
Walter Roberson
le 24 Fév 2013
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
le 24 Fév 2013
Walter Roberson
le 24 Fév 2013
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
le 24 Fév 2013
Walter Roberson
le 24 Fév 2013
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.
Andre Luis
le 24 Fév 2013
Andre Luis
le 25 Fév 2013
Réponses (2)
Muthu Annamalai
le 23 Fév 2013
Modifié(e) : Muthu Annamalai
le 23 Fév 2013
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 commentaire
Andre Luis
le 24 Fév 2013
Youssef Khmou
le 24 Fév 2013
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 commentaires
Andre Luis
le 24 Fév 2013
Youssef Khmou
le 24 Fév 2013
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
le 24 Fév 2013
Catégories
En savoir plus sur Common Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!