How to function a sum
1 vue (au cours des 30 derniers jours)
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
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.
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
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 ?
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!