Can you help me with this problem? (Fibonacci sequence in tia portal)

I think drbitboy has the right idea but it would take 3 ULINTs. I don't know how useful a 192 bit answer would be unless it is converted back to decimal. That could be done but it would take subtracting many powers of 10 in binary format so there would need to be 192 bit add and 192 bit subtract routines.
 
@mk42 raises a good point. Maybe the purpose of the exercise is not the task itself, but to see if you can recognize that the sequence cannot be generated with any atomic data type, in which case you have already plagiarized the answer from this site, which would be a problem if this was a course for some kind of certification. Or maybe the purpose is to see if you can use resources, e.g. the 'net, like plctalk. Maybe they are following this thread.

Personally, I would do the simplest, brute-force solution and find online the 100 decimal strings that contain the 150th through the 249th Fibonacci numbers in decimal, put them into an array of strings, and use your .Haneint value as the index/lookup into that array of strings. That does not solve the hexadecimal problem, but it's a start.

Also, to be fair, the analytical solution is not that complex if you know ladder logic (or structured text, which would be better for this kind of algorithm), and you should just tell them that, while you can get a solution from the OCD folks on plctalk, your knowledge of PLC programming is not yet to the point that you would understand it or could do something similar.
 
I think drbitboy has the right idea but it would take 3 ULINTs. I don't know how useful a 192 bit answer would be unless it is converted back to decimal. That could be done but it would take subtracting many powers of 10 in binary format so there would need to be 192 bit add and 192 bit subtract routines.
Nice catch, I mistakenly called them UDINTs instead of ULINTs, but I did refer to them as 64-bit.

That solves the hexadecimal string part of the problem. Converting to decimal would require code equivalent to the BigNumber library, or the Decimal module in Python, so at that point you might as well use the BigNumber code to calculate the necessary members of the sequence.

Also note that there is a non-iterative, one step formula for the n'th Fibonacci Number, but again that would require BigNumber capabilities, including square root.

Finally, if you take the iterative approach, it is possible that the calculation cannot complete in one scan cycle and would instead cause a watchdog timeout. If one or more of those terms are not in your wheelhouse, then I suggest you find out what resources are allowed for this exercise.

I assume you have some education (e.g. you can sum the values 12345 and 90752, or even 9 and 7, on paper, which is all that is required to know to solve this task). Philosophically though, the point of education is not skills like that, but rather that you learn that you can teach yourself anything.
 
I used an arrays of bytes to represent the digits of the result of the Fibonacci Number, easy to expand to large numbers of digits. Not implemented the hex representation though :(
 

Attachments

  • fib1.jpg
    fib1.jpg
    164.1 KB · Views: 8
.. code for Fibonacci 150
That's what I was envisioning, but in ladder, because ST/SCL is boring, of course ;).

Change the 9 and 10 in line numbers 16 and 18 to 15 and 16, respectively, for the hexadecimal digits, which only needs 47 (=55/log10(16)) of the 55 digits.

Line numbers 4 and 5 can be eliminated, but might be better to initialize the arrays with the digits for Fibonacci Numbers 149 and 150, and loop from there for the "ID number" of steps.
 
In case OP wants to do it the simple way i.e. via lookup into a string array that contains Fibonacci Numbers 150-249 in both decimal and hexadecimal, here are the necessary data, and the script to create them.
 

Attachments

  • fibonacci_tia.zip
    5.7 KB · Views: 2

Similar Topics

This is the problem, I need to solve it using a ladder diagram: Write a table of inputs and outputs and propose a control solution for a screw...
Replies
22
Views
4,072
Hey all, I am having an issue concerning the Modicon System Responsible for the operation of our Generator System. (Particularly Dresser-Rand...
Replies
5
Views
2,610
Replies
49
Views
10,965
please help i didn't solve this problem with rslogix. I counted but i am not using timer. How i will fix this situation? Please anyone solve this...
Replies
18
Views
5,012
Good day to every body I am trying to replace old touché screen [omron nt20s-st121-v1] To new one [[omron nt20s-st121b-v3] they look identical and...
Replies
2
Views
2,007
Back
Top Bottom