Borrar filtros
Borrar filtros

MacLaurin Series Demonstration - Unexpected Result?

1 visualización (últimos 30 días)
paul
paul el 14 de Sept. de 2022
Comentada: paul el 14 de Sept. de 2022
Hello,
I thought it might be interesting to show how the MacLaurin Series approximates the sin(x) function from first principles, as it were, and have written the script attached below.
The results were not what I expected, however...and the more I've thought about it, the less sure I am as to what I should have expected...
I'd be very grateful if anybody has the time to tell me if I'm on the right lines here, or can interpret my results for me - I've only recently encountered the MacLaurin Series, so I'm far from an expert, which no doubt shows!
One particular aspect of this series that confuses me is what the domain 'x' should be given in, i.e. radians or degrees? I've presumed radians, but my results are well outside the range of -1..1 I'd expect.
I appreciate this is rather more of a "how does maths work?" than "how does matlab work?" question, but your patience and forebearance would be greatly appreciated,
Thanks,
Paul
% MacLaurin Series Function Approximation
% f(x) = sin(x)
% Define vector to contain series x values
X = 0:(0.01*pi):(2*pi)
% Create matrix containing the results of each odd function
F = [X(1,:) ;...
(X(1,:).^3)/factorial(3) ; ...
(X(1,:).^5)/factorial(5) ; ...
(X(1,:).^7)/factorial(7) ; ...
(X(1,:).^9)/factorial(9) ; ...
(X(1,:).^11)/factorial(11) ; ...
(X(1,:).^13)/factorial(13) ; ...
(X(1,:).^15)/factorial(15) ; ...
(X(1,:).^17)/factorial(17) ; ...
(X(1,:).^19)/factorial(19) ; ...
(X(1,:).^21)/factorial(21) ; ...
]
% Create better approximations by progressively summing the function results
Y = [F(1,:)-F(2,:) ; ...
F(1,:)-F(2,:)+F(3,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:)-F(7,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:)-F(7,:)+F(8,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:)-F(7,:)+F(8,:)-F(9,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:)-F(7,:)+F(8,:)-F(9,:)+F(10,:) ; ...
]
% Plot each approximation of sin(x)
hold on
legend('Location','northwest','NumColumns',2)
plot(X(1,:),Y(1,:))
plot(X(1,:),Y(2,:))
plot(X(1,:),Y(3,:))
plot(X(1,:),Y(4,:))
plot(X(1,:),Y(5,:))
plot(X(1,:),Y(6,:))
plot(X(1,:),Y(7,:))
plot(X(1,:),Y(8,:))
plot(X(1,:),Y(9,:))
plot(X(1,:),Y(10,:))

Respuesta aceptada

Chunru
Chunru el 14 de Sept. de 2022
Editada: Chunru el 14 de Sept. de 2022
  • There was error in computing Y, which has been corrected.
  • You should use loops/array operation instead of hard coding
  • When X is small, it converges faster (within the number of terms you use)
  • When X is large, it requires more terms before converging.
format longg
X = 0*pi:(0.01*pi):(2*pi) % This will work for the number of terms
X = 1×201
0 0.0314159265358979 0.0628318530717959 0.0942477796076938 0.125663706143592 0.15707963267949 0.188495559215388 0.219911485751286 0.251327412287183 0.282743338823081 0.314159265358979 0.345575191894877 0.376991118430775 0.408407044966673 0.439822971502571 0.471238898038469 0.502654824574367 0.534070751110265 0.565486677646163 0.596902604182061 0.628318530717959 0.659734457253857 0.691150383789755 0.722566310325652 0.75398223686155 0.785398163397448 0.816814089933346 0.848230016469244 0.879645943005142 0.91106186954104
% Create matrix containing the results of each odd function
F = [X(1,:) ;...
(X(1,:).^3)/factorial(3) ; ...
(X(1,:).^5)/factorial(5) ; ...
(X(1,:).^7)/factorial(7) ; ...
(X(1,:).^9)/factorial(9) ; ...
(X(1,:).^11)/factorial(11) ; ...
(X(1,:).^13)/factorial(13) ; ...
(X(1,:).^15)/factorial(15) ; ...
(X(1,:).^17)/factorial(17) ; ...
(X(1,:).^19)/factorial(19) ; ...
(X(1,:).^21)/factorial(21) ; ...
]
F = 11×201
0 0.0314159265358979 0.0628318530717959 0.0942477796076938 0.125663706143592 0.15707963267949 0.188495559215388 0.219911485751286 0.251327412287183 0.282743338823081 0.314159265358979 0.345575191894877 0.376991118430775 0.408407044966673 0.439822971502571 0.471238898038469 0.502654824574367 0.534070751110265 0.565486677646163 0.596902604182061 0.628318530717959 0.659734457253857 0.691150383789755 0.722566310325652 0.75398223686155 0.785398163397448 0.816814089933346 0.848230016469244 0.879645943005142 0.91106186954104 0 5.16771278004997e-06 4.13417022403998e-05 0.000139528245061349 0.000330733617923198 0.000645964097506246 0.00111622596049079 0.00177252548355714 0.00264586894338559 0.00376726261665643 0.00516771278004997 0.00687822571024651 0.00892980768392635 0.0113534649777698 0.0141802038684571 0.0174410306326687 0.0211669515470847 0.0253889728883855 0.0301381009332514 0.0354453419583627 0.0413417022403998 0.0478581880560428 0.0550258056819721 0.062875561394868 0.0714384614714108 0.0807455121882808 0.0908277198221583 0.101716090649724 0.113441630947657 0.126035346992639 0 2.55016403987735e-10 8.16052492760751e-09 6.19689861690195e-08 2.6113679768344e-07 7.9692626246167e-07 1.98300755740862e-06 4.28606070182186e-06 8.35637752587009e-06 1.50584636390717e-05 2.55016403987734e-05 4.10706468786287e-05 6.3456241837076e-05 9.46858056858179e-05 0.000137153942458299 0.000193653081778186 0.000267404080827843 0.000362086826316813 0.000481870836450296 0.000631445862897626 0.00081605249276075 0.00104151275054271 0.00131426070011612 0.00164137304669163 0.00203059973878643 0.00249039457019272 0.00302994578194617 0.00365920666429443 0.00438892615866558 0.00523067945963662 0 5.99264529320792e-15 7.67058597530614e-13 1.31059152562457e-11 9.81835004839186e-11 4.68175413531869e-10 1.67755715279945e-09 4.93520108270433e-09 1.25674880619416e-08 2.86626366654094e-08 5.99264529320792e-08 1.16779703571088e-07 2.1472731555833e-07 3.76029605055827e-07 6.31705738586155e-07 1.0238996293942e-06 1.60863847192852e-06 2.45901411737464e-06 3.6688174931724e-06 5.35665626944993e-06 7.67058597530614e-06 1.07932847678744e-05 1.49478020570993e-05 2.04039111891591e-05 2.74850963914662e-05 3.65762041821773e-05 4.81317894471459e-05 6.26851863872504e-05 8.08583345390278e-05 0.000103372390071548 0 8.21458866111283e-20 4.20586939448977e-17 1.61687748616684e-15 2.15340512997876e-14 1.6044118478736e-13 8.27841272917421e-13 3.31488282497203e-12 1.10254342654913e-11 3.18249995602219e-11 8.21458866111282e-11 1.93695703659858e-10 4.2385473173372e-10 8.71116003062239e-10 1.69722000638568e-09 3.15796384016961e-09 5.64502234393152e-09 9.74150625617705e-09 1.62943997748336e-08 2.65074670325598e-08 4.20586939448976e-08 6.52468386439245e-08 9.91722002738472e-08 1.47957282297871e-07 2.17013622647664e-07 3.13361689037812e-07 4.46011393567866e-07 6.26411466343847e-07 8.68976643269469e-07 1.19170236838482e-06 0 7.37043094571435e-25 1.5094642576823e-21 1.30564973074046e-19 3.09138279973335e-18 3.59884323521208e-17 2.67397064855646e-16 1.45737502163958e-15 6.3311519738539e-15 2.3129193285148e-14 7.37043094571435e-14 2.10286996624478e-13 5.47629188824364e-13 1.32089944278939e-12 2.98470404431786e-12 6.37524282588116e-12 1.29661992424528e-11 2.52598645114092e-11 4.73685878479832e-11 8.58583409057711e-11 1.5094642576823e-10 2.58169612958386e-10 4.3066776908693e-10 7.02261852510741e-10 1.1215445787123e-09 1.7572476734434e-09 2.70520205883268e-09 4.09726720288412e-09 6.11267388276297e-09 8.99230147306484e-09 0 4.66302805767613e-30 3.81995258484828e-26 7.43437288199838e-24 3.12930515750771e-22 5.69217292196792e-21 6.09023826493307e-20 4.51796174008316e-19 2.56352678503032e-18 1.18527916763463e-17 4.66302805767612e-17 1.60980375354227e-16 4.98912318663317e-16 1.41231512001132e-15 3.70111425747612e-15 9.07516220947068e-15 2.10004114229684e-14 4.61853252668831e-14 9.70980694126288e-14 1.96094241793495e-13 3.81995258484828e-13 7.20309031533459e-13 1.31875123490183e-12 2.35033569779875e-12 4.08708971448989e-12 6.94845327388664e-12 1.15696854631328e-11 1.88971783838075e-11 3.03195279972444e-11 4.78455995779233e-11 0 2.19153534478302e-35 7.18122301778501e-31 3.14461368495045e-28 2.35314315846779e-26 6.68803510981146e-25 1.03042701228456e-23 1.04044488505716e-22 7.71077950166726e-22 4.51217693162813e-21 2.19153534478302e-20 9.15458700720459e-20 3.37650323385406e-19 1.12175693694662e-18 3.40932979935529e-18 9.59659938034196e-18 2.52666822710633e-17 6.27310128910544e-17 1.47855013695591e-16 3.32699764596115e-16 7.181223017785e-16 1.49292468943108e-15 2.9997750705208e-15 5.84340542684757e-15 1.1064125796693e-14 2.04102633966415e-14 3.67577313098669e-14 6.47448071594775e-14 1.11716918865274e-13 1.89111721371654e-13 0 7.95205400147552e-41 1.0422916220814e-35 1.02692954993535e-32 1.36615247489453e-30 6.06693573110619e-29 1.34601709969126e-27 1.84989040961728e-26 1.79064337189376e-25 1.32617849468168e-24 7.95205400147551e-24 4.01934206551172e-23 1.76425153290733e-22 6.87885745261439e-22 2.42468835769356e-21 7.83485069225579e-21 2.34703208040859e-20 6.57825923471491e-20 1.73824867654917e-19 4.35803322420815e-19 1.0422916220814e-18 2.38895149030112e-18 5.26823203210752e-18 1.12163753149665e-17 2.3124397692123e-17 4.62870462883469e-17 9.01625604029073e-17 1.71262906970287e-16 3.1780875241961e-16 5.77092306991634e-16 0 2.29484289972699e-46 1.20315859421206e-40 2.66720747517122e-37 6.30801613042255e-35 4.37706546731374e-33 1.39838487274257e-31 2.61586736808858e-30 3.30721716098698e-29 3.09999247288587e-28 2.29484289972699e-27 1.40350504469305e-26 7.33156408160457e-26 3.35487739833491e-25 1.37146787068042e-24 5.08729453119512e-24 1.73393427089954e-23 5.48633681552508e-23 1.62528885362439e-22 4.54016229947239e-22 1.20315859421206e-21 3.04032184471205e-21 7.3584085287203e-21 1.71230783667967e-20 3.8438510692163e-20 8.34858983481169e-20 1.75892196141821e-19 3.60300179922529e-19 7.19044146983298e-19 1.40060257369159e-18
% Create better approximations by progressively summing the function results
% Y = [F(1,:)-F(2,:) ; ...
% F(1,:)-F(2,:)+F(3,:) ; ...
% F(1,:)-F(2,:)+F(3,:)-F(4,:) ; ...
% F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:) ; ...
% F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:) ; ...
% F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:) ; ...
% F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:)-F(7,:) ; ...
% F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:)-F(7,:)+F(8,:) ; ...
% F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:)-F(7,:)+F(8,:)-F(9,:) ; ...
% F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(4,:)-F(5,:)+F(6,:)-F(7,:)+F(8,:)-F(9,:)+F(10,:) ; ...
% ]
Y = [F(1, :); ...
F(1,:)-F(2,:) ; ...
F(1,:)-F(2,:)+F(3,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(5,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(5,:)-F(6,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(5,:)-F(6,:)+F(7,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(5,:)-F(6,:)+F(7,:)-F(8,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(5,:)-F(6,:)+F(7,:)-F(8,:)+F(9,:) ; ...
F(1,:)-F(2,:)+F(3,:)-F(4,:)+F(5,:)-F(6,:)+F(7,:)-F(8,:)+F(9,:)-F(10,:)
]
Y = 10×201
0 0.0314159265358979 0.0628318530717959 0.0942477796076938 0.125663706143592 0.15707963267949 0.188495559215388 0.219911485751286 0.251327412287183 0.282743338823081 0.314159265358979 0.345575191894877 0.376991118430775 0.408407044966673 0.439822971502571 0.471238898038469 0.502654824574367 0.534070751110265 0.565486677646163 0.596902604182061 0.628318530717959 0.659734457253857 0.691150383789755 0.722566310325652 0.75398223686155 0.785398163397448 0.816814089933346 0.848230016469244 0.879645943005142 0.91106186954104 0 0.0314107588231179 0.0627905113695555 0.0941082513626325 0.125332972525669 0.156433668581983 0.187379333254897 0.218138960267728 0.248681543343798 0.278976076206425 0.308991552578929 0.338696966184631 0.368061310746849 0.397053579988903 0.425642767634114 0.4537978674058 0.481487873027282 0.508681778221879 0.535348576712911 0.561457262223698 0.586976828477559 0.611876269197814 0.636124578107782 0.659690748930784 0.68254377539014 0.704652651209168 0.725986370111188 0.746513925819521 0.766204312057485 0.785026522548401 0 0.0314107590781343 0.0627905195300804 0.0941083133316186 0.125333233662466 0.156434465508246 0.187381316262454 0.21814324632843 0.248689899721324 0.278991134670064 0.309017054219328 0.338738036831509 0.368124766988686 0.397148265794589 0.425779921576572 0.453991520487579 0.48175527710811 0.509043865048196 0.535830447549362 0.562088708086596 0.58779288097032 0.612917781948357 0.637438838807899 0.661332121977476 0.684574375128926 0.70714304577936 0.729016315893134 0.750173132483815 0.770593238216151 0.790257202008038 0 0.0314107590781283 0.0627905195293133 0.0941083133185127 0.125333233564283 0.15643446504007 0.187381314584897 0.218143241393229 0.248689887153836 0.278991106007427 0.309016994292875 0.338737920051806 0.36812455226137 0.397147889764984 0.425779289870834 0.453990496587949 0.481753668469638 0.509041406034079 0.535826778731869 0.562083351430326 0.587785210384344 0.612906988663589 0.637423891005841 0.661311718066287 0.684546890032535 0.707106469575178 0.728968184103687 0.750110447297428 0.770512379881612 0.790153829617966 0 0.0314107590781283 0.0627905195293134 0.0941083133185143 0.125333233564304 0.156434465040231 0.187381314585725 0.218143241396544 0.248689887164861 0.278991106039252 0.309016994375021 0.338737920245502 0.368124552685225 0.3971478906361 0.425779291568054 0.453990499745913 0.48175367411466 0.509041415775585 0.535826795026268 0.562083377937793 0.587785252443038 0.612907053910427 0.637423990178042 0.661311866023569 0.684547107046157 0.707106782936867 0.728968630115081 0.750111073708894 0.770513248858255 0.790155021320335 0 0.0314107590781283 0.0627905195293134 0.0941083133185143 0.125333233564304 0.156434465040231 0.187381314585725 0.218143241396543 0.248689887164855 0.278991106039229 0.309016994374947 0.338737920245291 0.368124552684677 0.397147890634779 0.425779291565069 0.453990499739538 0.481753674101694 0.509041415750325 0.5358267949789 0.562083377851935 0.587785252292092 0.612907053652258 0.637423989747374 0.661311865321307 0.684547105924613 0.707106781179619 0.728968627409878 0.750111069611627 0.770513242745581 0.790155012328033 0 0.0314107590781283 0.0627905195293134 0.0941083133185143 0.125333233564304 0.156434465040231 0.187381314585725 0.218143241396543 0.248689887164855 0.278991106039229 0.309016994374947 0.338737920245291 0.368124552684678 0.397147890634781 0.425779291565073 0.453990499739547 0.481753674101715 0.509041415750371 0.535826794978997 0.562083377852131 0.587785252292474 0.612907053652978 0.637423989748693 0.661311865323658 0.6845471059287 0.707106781186568 0.728968627421448 0.750111069630524 0.770513242775901 0.790155012375879 0 0.0314107590781283 0.0627905195293134 0.0941083133185143 0.125333233564304 0.156434465040231 0.187381314585725 0.218143241396543 0.248689887164855 0.278991106039229 0.309016994374947 0.338737920245291 0.368124552684678 0.397147890634781 0.425779291565073 0.453990499739547 0.481753674101715 0.509041415750371 0.535826794978997 0.562083377852131 0.587785252292473 0.612907053652977 0.63742398974869 0.661311865323652 0.684547105928689 0.707106781186547 0.728968627421411 0.750111069630459 0.770513242775789 0.79015501237569 0 0.0314107590781283 0.0627905195293134 0.0941083133185143 0.125333233564304 0.156434465040231 0.187381314585725 0.218143241396543 0.248689887164855 0.278991106039229 0.309016994374947 0.338737920245291 0.368124552684678 0.397147890634781 0.425779291565073 0.453990499739547 0.481753674101715 0.509041415750371 0.535826794978997 0.562083377852131 0.587785252292473 0.612907053652977 0.63742398974869 0.661311865323652 0.684547105928689 0.707106781186547 0.728968627421411 0.75011106963046 0.770513242775789 0.79015501237569 0 0.0314107590781283 0.0627905195293134 0.0941083133185143 0.125333233564304 0.156434465040231 0.187381314585725 0.218143241396543 0.248689887164855 0.278991106039229 0.309016994374947 0.338737920245291 0.368124552684678 0.397147890634781 0.425779291565073 0.453990499739547 0.481753674101715 0.509041415750371 0.535826794978997 0.562083377852131 0.587785252292473 0.612907053652977 0.63742398974869 0.661311865323652 0.684547105928689 0.707106781186547 0.728968627421411 0.75011106963046 0.770513242775789 0.79015501237569
% Plot each approximation of sin(x)
hold on
plot(X', Y');
hold on; plot(X, sin(X), '--')
legend('Location','best','NumColumns',2)
ylim([-5, 5])
% The above shows that with more terms included, it approach to sin(x)
% (dashed line)
figure % just the last summation
hold on
plot(X', Y(end,:));
hold on; plot(X, sin(X), '--')
legend('Location','best','NumColumns',2)
% Approximate well
  1 comentario
paul
paul el 14 de Sept. de 2022
Hi Chunru,
thanks very much indeed for your help with this - the perils of cutting and pasting! I'm rather ashamed I didn't spot that before I posted my question...
I will certainly look into loops/array operations - all the explicit coding I've done here seemed superfluous to me when I was doing it, but my knowldge of matlab syntax is quite rudimentary at the moment - I very much appreciate your advice.
Thanks again,
Paul

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by