Implementation of the method of Taylor 2nd Order in Scilab
rftg.development.googlepages.com
Creation date: April 9, 2007 version: v0.02 - 13/MAI/2008 Author: Ricardo Filipe Teixeira Gomes
LEGAL NOTICE
This document was prepared by Ricardo Filipe Teixeira Gomes, who was reserve all rights. © 2008 Ricardo Filipe Teixeira Gomes This document is available for consultation and use subject to compliance with all copyright and / or intellectual property. The copy or part, by any means, of texts and images available on this document is expressly prohibited unless you respect the rights of authorship and / or intellectual property, citing the document for it properly, and including imperterivelmente a clear reference to the author's website: "rftg.development.googlepages.com." The material contained herein is only an information of a general nature based on personal experiences and no way seeks to influence the reader on any particular issue. The content of this document are provided as a convenience for readers and consists solely of non-binding information. The content of this document are provided "as is" and does not offer any guarantee on it. The author of the document assumes no liability for any damage that may occur because of someone is based on information contained herein, since this information is for information only, does not promise or guarantee that is accurate, complete and updated. The same applies to the content of any reference in the same. Any disputes arising out of or related to use this document, or relating to copyright and / or intellectual property materials that are part of this document should be governed by Portuguese law and subject to the jurisdiction of the courts of Portugal. The reading of this document and its use requires the acceptance of these conditions.
© 2008 Ricardo Filipe Teixeira Gomes. rftg.development.googlepages.com
Polytechnic Institute of Porto Institute of Engineering of Porto
Department of Electrical Engineering Degree in Electrical Engineering, Systems and Automation branch Discipline of Applied Mathematics II
Implementation of the method of Taylor 2nd Order in Scilab
09-04-2007
Teacher / Advisor: Prof.. Jose Magalhaes Work done by: Ricardo Filipe Teixeira Gomes
Not be used because of the library's symbolic Scilab is necessary to ask the user to 1st and 2nd derivative, although it is not important for the demonstration in question, since the intent is to prove the validity and accuracy of increase, the method Taylor of the 2nd order concerning the method of Euler (from 1st class) in solving differential equations. The general formula for applying the method of Taylor's 2nd order was implemented:
Since the function has two variables, to be achieved''and you must run the following calculations:
y '' =
d 2 dF dx and dy = ? F = x * 2 + dx dx ? y dx dx
At that actually will request the user to the 1st derivative of the function
? ? F dy ? ? ? ? ? ? F = F (x) and the 2nd ? s partial derivatives in order to 'x' ? ? ? and in order 'and' ? ? ? y ?. ? ? ? x dx ? ? ? ? ?
Then you see the code developed:
/ / (C) 2008 Ricardo Gomes - ALL RIGHTS RESERVED / / http://rftg.development.googlepages.com / / Implementation of the method of Taylor 2nd order / / v0.01 09-04-2007 clear / erase all variables defined
g = input ( 'What is the actual function (only for the purpose of theoretical)?', 's'); g1 = input (' What 1st derivative of the function (enter so clear)? ',' s'); g2 = input ( 'What 2nd partial derivative of the function at x (enter so clear)?', 's'); g3 = input (' What 2nd partial derivative of the function and in (insert so clear)? ',' s' ); X0 = input ( 'What is the initial value of x? "); XF = input (' What is the final value of x pertendido? '); Y0 = input (' What is the initial value of y?") H = input ( 'What step of iteration h?'); / / Parameters used to test / / g = 'exp (x ^ 2)', / / g1 ='2 * x * y '; / / g2 ='2 * y '; / / g3 * x ='2'; / / x0 = 0; / / y0 = 1, / / XF = 2; / / h = 0.2;
/ / FUNCTIONS: function out = f (x) = evstr out (g) endfunction function out = f1 (x, y) = evstr out (g1) endfunction function out = f2 (x, y) = evstr out (g2) endfunction function out = f3 (x, y) = evstr out (g3) endfunction
/ / Initial values x (1) = x0; y (1) = y0; / / calculate the length of vectors (of data) to submit c_vect = (XF-x0) / h;
//*************************** Step h ******************* ******** / / Calculate the number of iterations n_iter = (XF - x0) / h / / in this case equals' c_vect 'for i = 1: n_iter x (i +1) = x (i) + h, and (i +1) = y (i) + h * f1 (x (i) and (i)) + ((h ^ 2) / 2) * (f2 (x (i) , and (i)) + f3 (x (i) and (i)) * f1 (x (i) and (i))); end / / copy to the vector output xsaida = x, y = y1 ; //*********************************************** ****************
//*************************** Step h / 2 ***************** ******** / / Calculate the number of iterations n_iter = (XF - x0) / (h / 2) for i = 1: n_iter x (i +1) = x (i) + (h / 2), y (i +1) = y (i) + (h / 2) * f1 (x (i) and (i)) + (((h / 2) ^ 2) / 2) * (f2 (x (i) and (i)) + f3 (x (i) and (i)) * f1 (x (i) and (i))); end / / copy to the vector output y2 ( 1) = y0; for i = 1: c_vect y2 (i +1) = y (2 * i + 1); end //******************** ******************************************* //***** ********************** Step h / 4 ************************ * / / Calculate the number of iterations n_iter = (XF - x0) / (h / 4) for i = 1: n_iter x (i +1) = x (i) + (h / 4);
and (i +1) = y (i) + (h / 4) * f1 (x (i) and (i)) + (((h / 4) ^ 2) / 2) * (f2 (x ( i) and (i)) + f3 (x (i) and (i)) * f1 (x (i) and (i))); end / / copy to the vector output Y4 (1) = y0; for i = 1: c_vect Y4 (i +1) = y (4 * i + 1); end //*********************** ****************************************
//************************** Calculation EC1 ******************** **** EC1 (1) = 0 for i = 1: c_vect EC1 (i +1) = y2 (i +1) - y1 (i +1); end //********* ************************************************** ****
//************************** Calculation EC2 ******************** **** EC2 (1) = 0 for i = 1: c_vect EC2 (i +1) = Y4 (i +1) - y2 (i +1); end //********* ************************************************** ****
//*********************** Calculation of Reason ********************** Grounds (1) = 0 for i = 1: c_vect if EC2 (i +1) == 0 grounds (i +1) = 0; else grounds (i +1) = EC1 (i +1) / EC2 (i 1) end end //****************************************** ********************* //******************* Calculation of the real solution **** ******************** For i = 1: c_vect 1 yreal (i) = f (xsaida (i)); end //****** ************************************************** ************ Printf ( "\ n \ n \ n") printf ("********************** *********** \ n ") printf (" Table method * Taylor 2nd order * \ n ") printf ("****************** *************** \ n ") printf (" \ parameters are presented, respectively: \ n ") printf (" \ nx; and (x), and for h, and to h / 2, and for h / 4; Error 1; Error 2; Reason \ n \ n ") = Table [xsaida yreal y1 y2 Y4 EC1 EC2 grounds] printf (" \ n \ nFim. \ n ")
Running the application in Scilab:
-> scipad () -> What is the actual function (only for the purpose of theoretical)? -> exp (x ^ 2) What 1st derivative of the function (enter so clear)? -> 2 * x * y What 2nd partial derivative of the function at x (enter so clear)? -> 2 * y What 2nd partial derivative of the function and in (insert so clear)? -> 2 x * What is the initial value of x? -> 0 What is the final value of x pertendido? -> 2 What is the initial value of y? -> 1 Which step of iteration h? -> 0.2
********************************* * Table method Taylor 2nd order * ********** *********************** The parameters are presented, respectively: x, y (x), and for h, and to h / 2, and for h / 4; Error 1; Error 2; Reason Table 0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. End. = 1. 1.0408108 1.1735109 1.4333294 1.8964809 2.7182818 4.2206958 7.0993271 12.935817 25.533722 54.59815 1. 1.04 1.168128 1.4167056 1.8541843 2.6166249 3.9772699 6.5036318 11.42558 21.534934 43.483338 1. 1.040502 1.1718627 1.4284046 1.8839578 2.6878302 4.1464242 6.9133307 12.451365 24.211098 50.794026 1. 1.0407196 1.1730583 1.4319937 1.8930814 2.7099647 4.2002253 7.0474891 12.799052 25.154888 53.490859 0. 0.000502 0.0037347 0.0116989 0.0297735 0.0712052 0.1691543 0.4096990 1.0257847 2.6761647 7.3106883 0. 0.0002176 0.0011956 0.0035891 0.0091236 0.0221345 0.0538011 0.1341584 0.3476866 0.9437900 2.6968328 0. 2.3069436 3.1236791 3.259548 3.2633545 3.2169335 3.1440693 3.053845 2.9503143 2.8355509 2.7108422
Compared with the figures obtained through the method of Euler (from 1st class) (see below) you can observe that there was an increase in accuracy of calculation due to the application of the method of Taylor's 2nd order, since there was a significant reduction of error of regarding the analytical value.
Euler's method: -> scipad () -> What is the function (enter so explicit) -> 2 * x * y What is the initial value of x? -> 0 What is the initial value of y? -> 1 Which step of iteration h? -> 0.2 What is the final value of x pertendido? -> 2 exit = 0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. 1. 1. 1.08 1.2528 1.553472 2.050583 2.8708163 4.2488081 6.6281406 10.870151 18.696659 1. 1.02 1.124448 1.3358442 1.7056059 2.3346334 3.4179032 5.340132 8.8859797 15.717521 29.498643 1. 1.0302757 1.1482993 1.3824406 1.7951229 2.5106623 3.7769489 6.1035986 10.582204 19.660012 39.092999 0. 0.02 0.044448 0.0830442 0.1521339 0.2840503 0.5470870 1.091324 2.2578391 4.8473703 10.801984 0. 0.0102757 0.0238513 0.0465964 0.0895170 0.1760290 0.3590457 0.7634665 1.6962242 3.9424907 9.5943562 0. 1.94633 1.8635484 1.7822037 1.699497 1.6136569 1.5237253 1.4294326 1.3310971 1.2295198 1.1258686