Jump to content
Macro Express Forums

Summing a converging series


Recommended Posts

Any mathematicians around?

A macro I wrote needed some trigonometry, so I took the easiest course of an excursion to Excel to use its ATAN2 function. That works fine. But out of curiosity I wondered if I could do it entirely within ME Pro. (The copy/pasting to and from Excel always seems slow.) So i went looking for either a formula or a convergent series delivering a suitable approximation.  I failed with ATAN2 but I did find a series for a good second best,  arctan (which in Excel is called ATAN):

arcatan(x) = x  - (x^3)/3 + (x^5)/5 - (x^7)/7... etc where x<1

It's slow to converge but that shouldn't be a problem with enough terms, I thought. But after some hours I gave up trying to implement it. Reckon I could have done it 40 years ago with BASIC or FORTRAN, but Repeats within Repeats in ME Pro (which was my approach) never fail to bring on a headache!

It's a long shot, but anyone ever written a similar macro they can share please?

Terry, East Grinstead, UK

 

Link to comment
Share on other sites

That would be a fun one to do but I don't have time. But in order to do trig, doesn't one need values tables?

https://www.w3schools.com/asp/func_sin.asp Could use an external script also.

Have you tried using a math web API? I was looking at one here where you do a GET request to http://api.mathjs.org/v4/?expr=2*(7-3) and if respond with plain text "8". Very fast. It must be URL encoded but they have a little tester you can build your expression with. 

Link to comment
Share on other sites

Brilliant, thanks Cory!

At first I tried it with x=1 (the tangent of 45 degrees, or ~ .7854 radians). So I tediously entered the series

1-1/3+1/5-1/7+1/9-1/11+1/13-1/15+1/17-1/19+1/21-1/23+1/25-1/27+1/29-1/31+1/33-1/35+1/37

and got 0.7985. About 1.7% high after 18 terms.

But then I realised that http://api.mathjs.org/ supports all the trig functions directly. And I was pleased to find that also includes atan2, which is the ideal function I need. And its result is returned very quickly.

@rberg:.I'm sure you're right, but I'd still like to try it! Or a better algorithm/formula if I can find one.

It's only a single calculation in a fairly long and partially user interactive macro, for tracing a few straight lines on a map, so a few seconds for each line each would be no problem.

But, academic curiosity apart, the only reason for pursuing it instead of simply using the handy web tool above would be to keep everything 'in-house', which I'd find sort of satisfying.

Terry, East Grinstead, UK

 

 

Link to comment
Share on other sites

Hmmm... I may have found a bug in MEP. In the above code, MEP seems to automatically create %Result% as an Integer. So the only result I seem to get, no matter how I change the value, is zero.

 

Can anyone replicate?

 

PS. Problem solved. The script worked fine after I manually created %Result% as a decimal variable.

Link to comment
Share on other sites

5 hours ago, acantor said:

Duh! In the decades that I've been using ME and ME Pro it's embarrassing that I've never been aware of the existence of those functions. Thanks for the heads up, Alan.

But unfortunately the one that would be most useful for my purposes, ATAN2, is not included.

Terry, East Grinstead, UK

 

 

 

5 hours ago, acantor said:

 

 

 

Link to comment
Share on other sites

Returning to the original subject, my macro for summing the series turned out to run much faster than I expected. For X = 1, which should give a result of 45 degrees, even ten thousand terms barely took one second . Its convergence is unacceptably slow though., so you'd never use it to actually calculate arctan(x).

Terms    Result    Duration (secs)
-----    -------   --------------
  100    44.8568   
 1,000   44.9857   
 5,000   44.9971
10,000   44.9986
10,001   45.0014   ~ 1.0
20,001   45.0007   ~ 1.2
50,001   45.0003   ~ 3
                       
                       

Here's the macro, code and upload. For my tests I disabled the user prompts.

// Set decimal value = dX and number of terms = nTerms
// Either by user prompting...
Variable Set Decimal %dX%: Prompt
Variable Set Integer %nTerms%: Prompt
// Or by hard coded values
Variable Set Decimal %dX% to 1
Variable Set Integer %nTerms% to 10001
Variable Set Integer %nSign% to -1
Repeat Start (Repeat %nTerms% times)
  Variable Modify Integer: %nExp% = 2 * %nT%
  Variable Modify Integer %nExp%: Decrement
  Extended Math %dResult%=%dX%^%nExp%
  Variable Modify Decimal: %dResult% = %dResult% / %nExp%
  Variable Modify Integer: %nSign% = -1 * %nSign%
  Variable Modify Decimal: %dResult% = %dResult% * %nSign%
  Variable Modify Decimal: %dResult% = %dResult% / 3.14159265358979323846264338328
  Variable Modify Decimal: %dResult% = %dResult% * 180
  Variable Modify Decimal: %dTotal% = %dResult% + %dTotal%
End Repeat
Text Box Display: Result of %nTerms% terms
<COMMENT Value="Set decimal value = dX and number of terms = nTerms"/>
<COMMENT Value="Either by user prompting..."/>
<VARIABLE SET DECIMAL Option="\x01" Destination="%dX%" Prompt="Enter X" Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0" _ENABLED="FALSE"/>
<VARIABLE SET INTEGER Option="\x01" Destination="%nTerms%" Prompt="How many terms?" Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0" _ENABLED="FALSE"/>
<COMMENT Value="Or by hard coded values"/>
<VARIABLE SET DECIMAL Option="\x00" Destination="%dX%" Value="1"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%nTerms%" Value="10001"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%nSign%" Value="-1"/>
<REPEAT START Start="1" Step="1" Count="%nTerms%" Save="TRUE" Variable="%nT%"/>
<VARIABLE MODIFY INTEGER Option="\x02" Destination="%nExp%" Value1="2" Value2="%nT%"/>
<VARIABLE MODIFY INTEGER Option="\x08" Destination="%nExp%"/>
<EXTENDED MATH Option="\x07" Destination="%dResult%" Value1="%dX%" Value2="%nExp%"/>
<VARIABLE MODIFY DECIMAL Option="\x03" Destination="%dResult%" Value1="%dResult%" Value2="%nExp%"/>
<VARIABLE MODIFY INTEGER Option="\x02" Destination="%nSign%" Value1="-1" Value2="%nSign%"/>
<VARIABLE MODIFY DECIMAL Option="\x02" Destination="%dResult%" Value1="%dResult%" Value2="%nSign%"/>
<VARIABLE MODIFY DECIMAL Option="\x03" Destination="%dResult%" Value1="%dResult%" Value2="3.14159265358979323846264338328"/>
<VARIABLE MODIFY DECIMAL Option="\x02" Destination="%dResult%" Value1="%dResult%" Value2="180"/>
<VARIABLE MODIFY DECIMAL Option="\x00" Destination="%dTotal%" Value1="%dResult%" Value2="%dTotal%"/>
<END REPEAT/>
<TEXT BOX DISPLAY Title="Result of %nTerms% terms" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\qc\\b\\f0\\fs20 \r\n\\par Sum of %nTerms% terms= \\f1 %dTotal%\\b0 \r\n\\par \\pard \r\n\\par \r\n\\par \r\n\\par }\r\n" Left="728" Top="485" Width="431" Height="144" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

 

SumSeriesForArctan(X).mex

Link to comment
Share on other sites

I thought of something else a couple days ago. I'm sure this is over but I'll put this here for future reference.

One can also launch Windows Calculator and using controls do calculations. 

Link to comment
Share on other sites

Terry if you ever need to do some wicked stuff like this I could write you a simple command line program you can command from MEP. Pass in the values as startup parameters and I'll return the result. Easy peasy. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...