[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: What did I do wrong with this funtion? - Implementing msplines
From: |
Corrado |
Subject: |
Re: What did I do wrong with this funtion? - Implementing msplines |
Date: |
Wed, 16 Sep 2009 10:33:11 +0100 |
User-agent: |
KMail/1.11.4 (Linux/2.6.28-13-generic; KDE/4.2.4; x86_64; ; ) |
Dear Jaroslav,
thanks for your help. I prototyped using R originally, and then rewrote it in
Octave. Two quick questions:
1) How can I make the function more efficient?
2) Do you think I should rather write it in Fortran or any other language,
instead of Octave?
Regards
On Wednesday 16 September 2009 10:26:43 Jaroslav Hajek wrote:
> On Wed, Sep 16, 2009 at 10:10 AM, Corrado <address@hidden> wrote:
> > Dear friends,
> >
> > I am trying to implement in Octave the two spline families as in the
> > documentation here below attached. I am a newbie to Octave, and this is
> > my first function.
> >
> > I built the function:
> >
> > function retspline = mspline (i,x,k,t)
> > # i is the spline index in the family
> > # x is the variable
> > # t is the vector of knots, t(h) is the h-th knot
> > # k is the Mspline degree
> >
> > I=i
> >
> > if(k==1)
> >
> > if(x<t(i+1) & x>=t(i))
> > td=t(i+1)-t(i)
> > M=1/td
> > else
> > M=0
> > endif
> >
> > else
> >
> > kk=(k-1)
> >
> > if (x>=t(i) & x<t(i+k))
> > M=k*((x-t(i))*mspline(i=I,x=x,k=kk,t=t)+(t(i+k)-
> > x)*mspline(i=(I+1),x=x,k=kk,t=t))/((k-1)*(t(i+k)-t(i)))
> > elseif (x<t(i) | x>=t(i+k))
> > M=0
> > endif
> >
> > endif
> >
> > retspline=M
> >
> > endfunction
> >
> > but for some reason it does not work ....
> >
> > I add that I am a newbie with Octave, please do not shut me.
> >
> > What did I do wrong?
> >
> > Best,
> > --
> > Corrado Topi
>
> You're probably misinterpreting the expression
> `mspline(i=I,x=x,k=kk,t=t)' (did you program in Python or Fortran?)
> In Octave, there is no passing parameters by name, and the expression
> is interpreted like in C, so that it means:
> i=I,x=x,k=kk,t=t,mspline(i,x,k,t)
> which probably spoils your script by overwriting variables. I didn't
> check for other problems.
> Note that even if you succeed, this will be a very inefficient
> implementation in Octave, but probably good enough to start
> programming.
>
> hth
--
Corrado Topi
Global Climate Change & Biodiversity Indicators
Area 18,Department of Biology
University of York, York, YO10 5YW, UK
Phone: + 44 (0) 1904 328645, E-mail: address@hidden