From: Jake on 11 Apr 2010 00:19 function fib(n) int n; if(n==0 ) return; else return n * fib(n-1); // line 7 I ran it >> fib(8) ??? Error: File: fib.m Line: 7 Column: 13 Missing MATLAB operator. Please help me
From: James Tursa on 11 Apr 2010 01:09 "Jake " <nathan.a2000(a)googlemail.com> wrote in message <hprijn$3bl$1(a)fred.mathworks.com>... > function fib(n) > > int n; > > if(n==0 ) return; > > else return n * fib(n-1); // line 7 > > > I ran it > > >> fib(8) > ??? Error: File: fib.m Line: 7 Column: 13 > Missing MATLAB operator. Looks like you took a C routine, called it an m-file, and ran it. There is no int data type in MATLAB. To return a variable you put it as part of the function line and then set it to a value in the routine ... it is not part of the return statement. Comments use % , not //. You should read the Getting Started and Functions section of the doc. James Tursa
From: Matt Fig on 11 Apr 2010 01:10 What you typed above is NOT MATLAB syntax. See the help for IF and RETURN. At the command line, type: >> help if >> help return Also, perhaps reading the 'Getting Started' section would help.
From: Roger Stafford on 11 Apr 2010 02:00 "Jake " <nathan.a2000(a)googlemail.com> wrote in message <hprijn$3bl$1(a)fred.mathworks.com>... > function fib(n) > int n; > if(n==0 ) return; > else return n * fib(n-1); // line 7 > ....... ------------------------- Besides the serious flaws pointed out by James and Matt, there are other difficulties with what you have written here, Jake. First of all, you seem to confuse the Fibonacci series with factorials. Where did you ever get such a strange notion? They are by no means the same entity. Secondly, if you eventually do manage to write a correct version using recursion, as you attempted to do with your present version, you will find that because the iteration involved with fibonacci numbers requires looking up two previous fibonacci numbers, then for a large number of terms out, the number of recursive calls will grow exponentially. The tenth term will need the ninth and eighth terms. These in turn will need the eighth and seventh and again the seventh and sixth. This goes on all the way back to the first two terms. For terms very far out in the series the number of these repeated calls for the same previous term becomes very large indeed. That is not an efficient way to do mathematics. The fibonacci series is not a good candidate for recursion and you should use a different method. Roger Stafford
From: Roger Stafford on 11 Apr 2010 04:59 "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hprohm$l1n$1(a)fred.mathworks.com>... > "Jake " <nathan.a2000(a)googlemail.com> wrote in message <hprijn$3bl$1(a)fred.mathworks.com>... > > function fib(n) > > int n; > > if(n==0 ) return; > > else return n * fib(n-1); // line 7 > > ....... > ------------------------- > Besides the serious flaws pointed out by James and Matt, there are other difficulties with what you have written here, Jake. First of all, you seem to confuse the Fibonacci series with factorials. Where did you ever get such a strange notion? They are by no means the same entity. > > Secondly, if you eventually do manage to write a correct version using recursion, as you attempted to do with your present version, you will find that because the iteration involved with fibonacci numbers requires looking up two previous fibonacci numbers, then for a large number of terms out, the number of recursive calls will grow exponentially. The tenth term will need the ninth and eighth terms. These in turn will need the eighth and seventh and again the seventh and sixth. This goes on all the way back to the first two terms. For terms very far out in the series the number of these repeated calls for the same previous term becomes very large indeed. That is not an efficient way to do mathematics. The fibonacci series is not a good candidate for recursion and you should use a different method. > > Roger Stafford I confess I was a little too disparaging of the recursion method. In point of fact there is a way of making recursive calls for generating the fibonacci series which avoids the difficulties I previously mentioned. You simply have to require that each call return a pair of consecutive fibonacci numbers.. Roger Stafford
|
Next
|
Last
Pages: 1 2 Prev: request for Matlab coding Next: MATLAB Error in ==> sym.sym>sym.gt at 792 <-------HELP PLEASE |