View previous topic :: View next topic |
Author |
Message |
dasha
Joined: 16 Nov 2015 Posts: 11
|
Posted: Tue Feb 02, 2016 12:12 am Post subject: mathematical equation |
|
|
Hello everyone
How do I write this equation in BASCOM AVR???
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Tue Feb 02, 2016 7:53 am Post subject: |
|
|
As you probably know, Bascom can only handle expressions one piece at a time, so you must break your complex expressions down into small pieces, evaluate each piece, then combine them back together, using temporary variables to hold the intermediate results.
To give you a start, just look at the first term:
Term1 = r^2 * pi
Pi is just a constant, so we define it as one:
note that the constant pi is declared in the main program, constants must be global.
you could just use an explicit number here, but its much more efficient to use globals
and I would bet that you use pi at least somewhere else in this program.
declaring it once saves program memory space
Code: |
Const pi = 3.141592
Dim Term1 as single 'declare a variable to hold the result of term 1
Dim Term2 as single 'and the second term
'note that we can re-use these temporary variables during the calculation
'if that makes it hard to read, use more named variables, as you like
Term1 = r * r 'put r^2 in term1, its much faster to just multiply than use the power function
Term1 = Term1 * pi 'multiply the result so far by pi
'note that this re-uses Term1 to hold the result we want
|
now you need to do term2, the second piece:
Term2 = r^2 arccos((r-h)/r)
Code: |
'do the arc cos bit first:
Term2 = r-h 'the numerator
Term2 = Term2/r 'div by the denominator
Term2 = acos(Term2) 'take the arc cos
Term2 = r * Term2 'mult by r
Term2 = r * Term2 'and again, to square r
'now you can subtract Term1 and Term2, putting the result back in Term1
Term1 = Term1 - Term2
'that leaves Term2 free to use it to calculate the last term
Term2 = .... your code for term3
|
Then you add the last term on:
Code: |
Term1 = Term1 + Term2 'add the last term on
V = Term1 * l 'multiply by the length, to get the volume
|
When doing this sort of thing, the Bascom simulator is really handy to run bits of code like this with test values, and see that the intermediate and final results agree with what you expect.
Writing the whole piece as a function makes it really easy to test in a very simple main program. Then when you have it correct, just copy the whole function into your main code.
Note that in a function, you can declare local variables to hold those intermediate values, so they are not 'seen' by the other parts of the program. That allows to create more of them, if you like readability, without using up extra global memory.
So in a function you would do it like this:
Code: |
Function ComputeVolume(Radius as single, Height as single, Length as single) as single
local Term1 as single
local Term2 as single
'note that we can re-use these temporary variables during the calculation
'if that makes it hard to read, use more named variables, as you like
Term1 = radius * radius 'put r^2 in term1
Term1 = Term1 * pi 'multiply the result so far by pi
Term2 = radius-height 'the numerator
Term2 = Term2/radius 'div by the denominator
Term2 = acos(Term2) 'take the arc cos
Term2 = radius * Term2 'mult by r
Term2 = radius * Term2 'and again, to square r
'now you can subtract Term1 and Term2, putting the result back in Term1
Term1 = Term1 - Term2
'compute the third term into Term2
Term1 = Term1 + Term2 'then add it on to the previous result
Term2 = Term1 * Length 'finally multiply by the length
ComputeVolume = Term2 'assign the result to the function for return
End Function
|
Of course you could also choose to create functions for each of those intermediate calculations too, and then combine the calls to get a single result. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Tue Feb 02, 2016 11:09 am Post subject: |
|
|
Adrian said all
I add
You must increase the size of stack
from the help (memory usage and local)
Quote: |
The software stack is used to store the addresses of LOCAL variables and for variables that are passed to SUB routines.
Each LOCAL variable and passed variable to a SUB/FUNCTION, requires two bytes to store the address (because it is a 16-Bit address = 2 bytes).
So when you have a SUB routine in your program that passes 10 variables, you need 10 * 2 = 20 bytes.
When you use 2 LOCAL variables in the SUB program that receives the 10 variables, you need additional 2 * 2 = 4 bytes.
|
Use Double if you need precision
jp |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Tue Feb 02, 2016 10:50 pm Post subject: |
|
|
Good point Duval.
Judging by the OP's question, he is having trouble translating from math theory notation to Bascom language notation and implementation.
The finer details about how stuff is stored, accessed, and represented can be the subject of a later discussion. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
dasha
Joined: 16 Nov 2015 Posts: 11
|
Posted: Thu Feb 04, 2016 11:31 am Post subject: |
|
|
Thanks AdrianJ ;Thanks Duval JP |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|