How to function a sum

1 次查看(过去 30 天)
Andre Luis
Andre Luis 2013-2-23
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 个评论
Andre Luis
Andre Luis 2013-2-24
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?

请先登录,再进行评论。

回答(2 个)

Muthu Annamalai
Muthu Annamalai 2013-2-23
编辑:Muthu Annamalai 2013-2-23
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
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 个评论
Youssef  Khmou
Youssef Khmou 2013-2-24
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.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by