Program for Picard’s iterative method | Computational Mathematics

• Difficulty Level : Medium
• Last Updated : 01 Sep, 2022

The Picardâ€™s method is an iterative method and is primarily used for approximating solutions to differential equations. This method of solving a differential equation approximately is one of successive approximation; that is, it is an iterative method in which the numerical results become more and more accurate, the more times it is used. The Picardâ€™s iterative method gives a sequence of approximations Y1(x), Y2(x), …Yk(x) to the solution of differential equations such that the nth approximation is obtained from one or more previous approximations. The Picardâ€™s iterative series is relatively easy to implement and the solutions obtained through this numerical analysis are generally power series. Picard’s iteration method formula:

Picard’s iteration formula.

Steps involved:

• Step 1: An approximate value of y (taken, at first, to be a constant) is substituted into the right hand side of the differential equation: dy/dx= f(x, y).
• Step 2: The equation is then integrated with respect to x giving y in terms of x as a second approximation, into which given numerical values are substituted and the result rounded off to an assigned number of decimal places or significant figures.
• Step 3: The iterative process is continued until two consecutive numerical solutions are the same when rounded off to the required number of decimal places.

Picard’s iteration example: Given that: and that y = 0 when x = 0, determine the value of y when x = 0.3, correct to four places of decimals. Solution: We may proceed as follows: where x0 = 0. Hence: where y0 = 0. which becomes:

• First Iteration: We do not know y in terms of x yet, so we replace y by the constant value y0 in the function to be integrated. The result of the first iteration is thus given, at x = 0.3, by:
• Second Iteration: Now, we use: Therefore, which gives: The result of the second iteration is thus given by: at x=0.3.
• Third Iteration: Now we use: Therefore, which gives: The result of the third iteration is thus given by: at x = 0.3.
• Hence, y = 0.0451, correct upto four decimal places, at x = 0.3.

Program for Picard’s iterative method:

C

 `// C program for Picard's iterative method``  ` `#include ``#include ``  ` `// required macros defined below:``#define Y1(x) (1 + (x) + pow(x, 2) / 2)``#define Y2(x) (1 + (x) + pow(x, 2) / 2 + pow(x, 3) / 3 + pow(x, 4) / 8)``#define Y3(x) (1 + (x) + pow(x, 2) / 2 + pow(x, 3) / 3 + pow(x, 4) / 8 + pow(x, 5) / 15 + pow(x, 6) / 48)``  ` `int` `main()``{``    ``double` `start_value = 0, end_value = 3,``           ``allowed_error = 0.4, temp;``    ``double` `y1[30], y2[30], y3[30];``    ``int` `count;``  ` `    ``for` `(temp = start_value, count = 0;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {``  ` `        ``y1[count] = Y1(temp);``        ``y2[count] = Y2(temp);``        ``y3[count] = Y3(temp);``    ``}``  ` `    ``printf``(``"\nX\n"``);``    ``for` `(temp = start_value;``         ``temp <= end_value;``         ``temp = temp + allowed_error) {``  ` `        ``// considering all values``        ``// upto 4 decimal places.``        ``printf``(``"%.4lf "``, temp);``    ``}``  ` `    ``printf``(``"\n\nY(1)\n"``);``    ``for` `(temp = start_value, count = 0;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {``  ` `        ``printf``(``"%.4lf "``, y1[count]);``    ``}``  ` `    ``printf``(``"\n\nY(2)\n"``);``    ``for` `(temp = start_value, count = 0;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {``  ` `        ``printf``(``"%.4lf "``, y2[count]);``    ``}``  ` `    ``printf``(``"\n\nY(3)\n"``);``    ``for` `(temp = start_value, count = 0;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {``  ` `        ``printf``(``"%.4lf "``, y3[count]);``    ``}``    ``return` `0;``}`

Java

 `// Java program for Picard's iterative method``import` `java.util.*;``class` `GFG {` `  ``// required macros defined below:``  ``static` `double` `Y1(``double` `x)``  ``{``    ``return` `(``1` `+ (x) + Math.pow(x, ``2``) / ``2``);``  ``}``  ``static` `double` `Y2(``double` `x)``  ``{``    ``return` `(``1` `+ (x) + Math.pow(x, ``2``) / ``2``            ``+ Math.pow(x, ``3``) / ``3` `+ Math.pow(x, ``4``) / ``8``);``  ``}``  ``static` `double` `Y3(``double` `x)``  ``{``    ``return` `1` `+ (x) + Math.pow(x, ``2``) / ``2``      ``+ Math.pow(x, ``3``) / ``3` `+ Math.pow(x, ``4``) / ``8``      ``+ Math.pow(x, ``5``) / ``15` `+ Math.pow(x, ``6``) / ``48``;``  ``}` `  ``public` `static` `void` `main(String[] args)``  ``{``    ``double` `start_value = ``0``, end_value = ``3``,``    ``allowed_error = ``0.4``, temp;``    ``double` `y1[] = ``new` `double``[``30``];``    ``double` `y2[] = ``new` `double``[``30``];``    ``double` `y3[] = ``new` `double``[``30``];``    ``int` `count;` `    ``for` `(temp = start_value, count = ``0``;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {` `      ``y1[count] = Y1(temp);``      ``y2[count] = Y2(temp);``      ``y3[count] = Y3(temp);``    ``}` `    ``System.out.printf(``"\nX\n"``);``    ``for` `(temp = start_value; temp <= end_value;``         ``temp = temp + allowed_error) {` `      ``// considering all values``      ``// upto 4 decimal places.``      ``System.out.printf(``"%.4f "``, temp);``    ``}` `    ``System.out.printf(``"\n\nY(1)\n"``);``    ``for` `(temp = start_value, count = ``0``;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {` `      ``System.out.printf(``"%.4f "``, y1[count]);``    ``}` `    ``System.out.printf(``"\n\nY(2)\n"``);``    ``for` `(temp = start_value, count = ``0``;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {` `      ``System.out.printf(``"%.4f "``, y2[count]);``    ``}` `    ``System.out.printf(``"\n\nY(3)\n"``);``    ``for` `(temp = start_value, count = ``0``;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {` `      ``System.out.printf(``"%.4f "``, y3[count]);``    ``}``  ``}``}` `// This code is contributed by phasing17`

Python3

 `# Python3 program for Picard's iterative method` `import` `math``  ` `# required macros defined below:``def` `Y1(x):``     ``return` `(``1` `+` `(x) ``+` `pow``(x, ``2``) ``/` `2``)``def` `Y2(x):``     ``return` `(``1` `+` `(x) ``+` `pow``(x, ``2``) ``/` `2` `+` `pow``(x, ``3``) ``/` `3` `+` `pow``(x, ``4``) ``/` `8``)``def` `Y3(x) :``     ``return` `(``1` `+` `(x) ``+` `pow``(x, ``2``) ``/` `2` `+` `pow``(x, ``3``) ``/` `3` `+` `pow``(x, ``4``) ``/` `8` `+` `pow``(x, ``5``) ``/` `15` `+` `pow``(x, ``6``) ``/` `48``)``  ` `start_value ``=` `0``end_value ``=` `3``allowed_error ``=` `0.4``y1 ``=` `[``0` `for` `_ ``in` `range``(``30``)]``y2 ``=` `[``0` `for` `_ ``in` `range``(``30``)]``y3 ``=` `[``0` `for` `_ ``in` `range``(``30``)]` `temp ``=` `start_value``count ``=` `0``while` `temp <``=` `end_value:` `    ``y1[count] ``=` `Y1(temp);``    ``y2[count] ``=` `Y2(temp);``    ``y3[count] ``=` `Y3(temp);``    ` `    ``temp ``=` `temp ``+` `allowed_error``    ``count ``+``=` `1``  ` `print``(``"\nX"``);``temp ``=` `start_value``while` `temp <``=` `end_value:``    ` `    ``# considering all values``    ``# upto 4 decimal places.``    ``print``(``round``(temp, ``4``), end ``=` `" "``)``    ``temp ``=` `temp ``+` `allowed_error``        ` `print``(``"\n\nY(1)"``);``temp ``=` `start_value``count ``=` `0``while` `temp <``=` `end_value:``    ``print``(``round``(y1[count], ``4``), end ``=` `" "``)` `    ``temp ``=` `temp ``+` `allowed_error``    ``count ``+``=` `1``  ` `print``(``"\n\nY(2)"``);``temp ``=` `start_value``count ``=` `0``while` `temp <``=` `end_value:``    ``print``(``round``(y2[count], ``4``), end ``=` `" "``)` `    ``temp ``=` `temp ``+` `allowed_error``    ``count ``+``=` `1` `print``(``"\n\nY(3)"``);``temp ``=` `start_value``count ``=` `0``while` `temp <``=` `end_value:``    ``print``(``round``(y3[count], ``4``), end ``=` `" "``)` `    ``temp ``=` `temp ``+` `allowed_error``    ``count ``+``=` `1``    `  `# This code is contributed by phasing17`

C#

 `// C# program for Picard's iterative method` `using` `System;``using` `System.Collections.Generic;` `class` `GFG {` `    ``// required macros defined below:``    ``static` `double` `Y1(``double` `x)``    ``{``        ``return` `(1 + (x) + Math.Pow(x, 2) / 2);``    ``}``    ``static` `double` `Y2(``double` `x)``    ``{``        ``return` `(1 + (x) + Math.Pow(x, 2) / 2``                ``+ Math.Pow(x, 3) / 3 + Math.Pow(x, 4) / 8);``    ``}``    ``static` `double` `Y3(``double` `x)``    ``{``        ``return` `1 + (x) + Math.Pow(x, 2) / 2``            ``+ Math.Pow(x, 3) / 3 + Math.Pow(x, 4) / 8``            ``+ Math.Pow(x, 5) / 15 + Math.Pow(x, 6) / 48;``    ``}` `    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``double` `start_value = 0, end_value = 3,``               ``allowed_error = 0.4, temp;``        ``double``[] y1 = ``new` `double``[30];``        ``double``[] y2 = ``new` `double``[30];``        ``double``[] y3 = ``new` `double``[30];``        ``int` `count;` `        ``for` `(temp = start_value, count = 0;``             ``temp <= end_value;``             ``temp = temp + allowed_error, count++) {` `            ``y1[count] = Y1(temp);``            ``y2[count] = Y2(temp);``            ``y3[count] = Y3(temp);``        ``}` `        ``Console.Write(``"\nX\n"``);``        ``for` `(temp = start_value; temp <= end_value;``             ``temp = temp + allowed_error) {` `            ``// considering all values``            ``// upto 4 decimal places.``            ``Console.Write(Math.Round(temp, 4) + ``" "``);``        ``}` `        ``Console.Write(``"\n\nY(1)\n"``);``        ``for` `(temp = start_value, count = 0;``             ``temp <= end_value;``             ``temp = temp + allowed_error, count++) {` `            ``Console.Write(y1[count] + ``" "``);``        ``}` `        ``Console.Write(``"\n\nY(2)\n"``);``        ``for` `(temp = start_value, count = 0;``             ``temp <= end_value;``             ``temp = temp + allowed_error, count++) {` `            ``Console.Write(Math.Round(y2[count], 4) + ``" "``);``        ``}` `        ``Console.Write(``"\n\nY(3)\n"``);``        ``for` `(temp = start_value, count = 0;``             ``temp <= end_value;``             ``temp = temp + allowed_error, count++) {` `            ``Console.Write(Math.Round(y3[count], 4) + ``" "``);``        ``}``    ``}``}` `// This code is contributed by phasing17`

Javascript

 `// JavaScript program for Picard's iterative method` `  ` `// required macros defined below:``function` `Y1(x)``    ``{ ``return` `(1 + (x) + Math.pow(x, 2) / 2) }``function` `Y2(x)``    ``{ ``return` `(1 + (x) + Math.pow(x, 2) / 2 + Math.pow(x, 3) / 3 + Math.pow(x, 4) / 8) }``function` `Y3(x)``    ``{ ``return` `(1 + (x) + Math.pow(x, 2) / 2 + Math.pow(x, 3) / 3 + Math.pow(x, 4) / 8 + Math.pow(x, 5) / 15 + Math.pow(x, 6) / 48)}``  ` `let start_value = 0, end_value = 3,``           ``allowed_error = 0.4, temp;``let y1 = ``new` `Array(30);``let y2 = ``new` `Array(30);``let y3 = ``new` `Array(30);``let count;``  ` `    ``for` `(temp = start_value, count = 0;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {``  ` `        ``y1[count] = Y1(temp);``        ``y2[count] = Y2(temp);``        ``y3[count] = Y3(temp);``    ``}``  ` `    ``console.log(``"\nX"``);``    ``for` `(temp = start_value;``         ``temp <= end_value;``         ``temp = temp + allowed_error) {``  ` `        ``// considering all values``        ``// upto 4 decimal places.``        ``console.log(temp.toFixed(4));``    ``}``  ` `    ``console.log(``"\n\nY(1)"``);``    ``for` `(temp = start_value, count = 0;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {``        ``console.log((y1[count]).toFixed(4));``    ``}``  ` `    ``console.log(``"\n\nY(2)"``);``    ``for` `(temp = start_value, count = 0;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {``        ` `        ``console.log((y2[count]).toFixed(4));``    ``}``  ` `    ``console.log(``"\n\nY(3)"``);``    ``for` `(temp = start_value, count = 0;``         ``temp <= end_value;``         ``temp = temp + allowed_error, count++) {``    ` `        ``console.log((y3[count]).toFixed(4));``    ``}` `// This code is contributed by phasing17`

Output:

```X
0.0000 0.4000 0.8000 1.2000 1.6000 2.0000 2.4000 2.8000

Y(1)
1.0000 1.4800 2.1200 2.9200 3.8800 5.0000 6.2800 7.7200

Y(2)
1.0000 1.5045 2.3419 3.7552 6.0645 9.6667 15.0352 22.7205

Y(3)
1.0000 1.5053 2.3692 3.9833 7.1131 13.1333 24.3249 44.2335```

My Personal Notes arrow_drop_up