PDA

View Full Version : Recursive Matlab problem


Gemma82au
Mar 11, 2003, 12:26 AM
Hi, I'm trying to write a matlab program for a recursive sequence. I worked out the sequence right I think but can't get the figures right in the program. Here's the program I have so far:

%recursive program for water
%dam cycling down to empty

Clear
Format long
A=32;
Fprintf(' Week Megalitres\n\n');

For n=1:10;
A=A+9-3*n;

Disp([n A])
End

These are what I get-

Week Megalitres

1 38

2 41

3 41

4 38

5 32

6 23

7 11

8 -4

9 -22

10 -43

But these are the figures I require-

1 38

2 42

3 44

4 44

5 42

6 38

7 32

8 24

9 14

10 2

The problem is where I have 3*n it should be be going up by 2(n+1) at a time. Ie 3,5,7,9 whereas mine is going up as 3,6,9,12.

Can anybody see where I've got it wrong?
Thank you

dtecmeister
Mar 22, 2003, 11:02 AM
I'm not used to Matlab, but I think you need to do the following:
Clear
Format long
A=38;
Fprintf(' Week Megalitres\n\n');

For n=1:10;
Disp([n A])
A=A-(2*(n-4))

End

renut
Jun 30, 2010, 11:57 PM
The equation u hv used A = A+9-3(n) is not gvng right answer. As I solved manually then I got
1 38
2 35
3 29
4 26
.
.
.

. So prblem is related with the equation. Check it.