The Wayback Machine - https://web.archive.org/web/20240602045645/https://www.geeksforgeeks.org/program-for-picards-iterative-method-computational-mathematics/
Open In App

Program for Picard’s iterative method | Computational Mathematics

Last Updated : 10 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
 
// required macros defined below:
double Y1(double x) { return (1 + (x) + pow(x, 2) / 2); }
double Y2(double x)
{
    return (1 + (x) + pow(x, 2) / 2 + pow(x, 3) / 3
            + pow(x, 4) / 8);
}
double Y3(double x)
{
    return 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];
    double y2[30];
    double 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);
    }
 
    cout << "\nX\n";
    for (temp = start_value; temp <= end_value;
         temp = temp + allowed_error) {
        cout << fixed << setprecision(4) << temp << " ";
    }
 
    cout << "\n\nY(1)\n";
    for (temp = start_value, count = 0; temp <= end_value;
         temp = temp + allowed_error, count++) {
        cout << fixed << setprecision(4) << y1[count]
             << " ";
    }
 
    cout << "\n\nY(2)\n";
    for (temp = start_value, count = 0; temp <= end_value;
         temp = temp + allowed_error, count++) {
        cout << fixed << setprecision(4) << y2[count]
             << " ";
    }
 
    cout << "\n\nY(3)\n";
    for (temp = start_value, count = 0; temp <= end_value;
         temp = temp + allowed_error, count++) {
        cout << fixed << setprecision(4) << y3[count]
             << " ";
    }
    return 0;
}
 
// This code is contributed by abn95knd1.


C




// C program for Picard's iterative method
   
#include <math.h>
#include <stdio.h>
   
// 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


Similar Reads

Program for Gauss Seidel Method (Computational Mathematics)
The Gauss Seidel method is an iterative process to solve a square system of (multiple) linear equations. It is also prominently known as 'Liebmann' method. In any iterative method in numerical analysis, every solution attempt is started with an approximate solution of an equation and iteration is performed until the desired accuracy is obtained. In
3 min read
Iterative Deepening Search(IDS) or Iterative Deepening Depth First Search(IDDFS)
There are two common ways to traverse a graph, BFS and DFS. Considering a Tree (or Graph) of huge height and width, both BFS and DFS are not very efficient due to following reasons. DFS first traverses nodes going through one adjacent of root, then next adjacent. The problem with this approach is, if there is a node close to root, but not in first
10 min read
Computational Complexity v/s Chomsky Hierarchy
1. Computational complexity :Computational complexity, a measure of the number of computing resources(time and space) that a particular algorithm consumes when it runs. The time complexity of a Turing machine is given by the function T(n), where T(n) = Maximum number of moves made by the TM to process a string of length n. The space complexity of t
4 min read
Computational Geometry - Algorithms for Geometry
Computational geometry is a field of study that focuses on developing algorithms and data structures for solving problems that involve geometric shapes and structures. The field has applications in a variety of areas, including computer graphics, robotics, geographic information systems, and more. In this article, we will explore some of the key co
22 min read
C Program for Iterative Merge Sort
Following is a typical recursive implementation of Merge Sort that uses last element as pivot. C/C++ Code /* Recursive C program for merge sort */ #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; /* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */ void merge(int arr[], int l, int m, int r); /* l is for left index and r
3 min read
Java Program for Iterative Merge Sort
Following is a typical recursive implementation of Merge Sort that uses last element as pivot. Java Code // Recursive Java Program for merge sort import java.util.Arrays; public class GFG { public static void mergeSort(int[] array) { if(array == null) { return; } if(array.length &amp;gt; 1) { int mid = array.length / 2; // Split left part int[] lef
2 min read
Python Program for Iterative Merge Sort
Following is a typical recursive implementation of Merge Sort that uses last element as pivot. Python] Python Program for Iterative Merge SortThe provided Python code demonstrates the recursive implementation of the Merge Sort algorithm. Merge Sort divides an array into smaller subarrays, sorts them, and then merges them back together to achieve a
2 min read
C Program for Iterative Quick Sort
C/C++ Code // An iterative implementation of quick sort #include &amp;lt;stdio.h&amp;gt; // A utility function to swap two elements void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } /* This function is same in both iterative and recursive*/ int partition(int arr[], int l, int h) { int x = arr[h]; int i = (l - 1); for (int j = l; j &amp;lt;
4 min read
Iterative program to find distance of a node from root
Given the root of a binary tree and a key x in it, find the distance of the given key from the root node. Dis­tance means num­ber of edges between two nodes. Examples: Input : x = 45, 5 is Root of below tree 5 / \ 10 15 / \ / \ 20 25 30 35 \ 45 Output : Distance = 3 There are three edges on path from root to 45. For more understanding of question,
9 min read
Iterative program to Calculate Size of a tree
Size of a tree is the number of elements present in the tree. Size of the below tree is 5. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach The idea is to use Level Order Traversing 1) Create an empty queue q 2) temp_node = root /*start from root*/ 3) Loop while temp_node is not NULL a) Enqueue temp_node’
5 min read
Practice Tags :