Several numeric calculation questions Objective C language implementation

Source: Internet
Author: User

Note: For personal habits, the c ++ statement is included.
1. Use the Newton method to find the solid root nearby and take four valid digits.

# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
# Include <cmath>
# Include <algorithm>
 
Using namespace std;
 
Const double eps = 1e-4;
Const double x0 = 2;
 
Double FunOfSet (double x)
{
Return pow (x, 3.0)-3 * X-1;
}
 
Void ProGetRes ()
{
Double Begin = x0-1, End = x0 + 1;
Double Mid;
While (fabs (FunOfSet (Begin)-FunOfSet (End)> eps)
{
Mid = (Begin + End)/2;
If (FunOfSet (Begin) * FunOfSet (Mid) <0)
End = Mid;
Else Begin = Mid;
}
Printf ("The root of fuction beside 2 is: %. 4f \ n", Mid );
}
Int main ()
{
ProGetRes ();
Return 0;
}

Ii. Use Gaussian elimination to solve the following equations

# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
# Include <cmath>
 
Using namespace std;
 
Const int LenOfMatrix = 3;
 
Double a [LenOfMatrix + 1] [LenOfMatrix + 2] = {0, 0}, {,-1,-}, {, 4, -}, {,-, 11 }};
Double Res [LenOfMatrix + 1];
Void GussPro ()
{
Int I, j, k, MaxPos, n = 3;
Double mmax;
For (k = 1; k <n-1; k ++)
{
Mmax =-1;
For (I = k; I <= n; I ++)
If (fabs (a [I] [k])> mmax)
{
Mmax = a [I] [k];
MaxPos = I;
}
Double temp;
For (j = 1; j <= n + 1; j ++)
{
Temp = a [k] [j];
A [k] [j] = a [MaxPos] [j];
A [MaxPos] [j] = temp;
}
For (I = k + 1; I <= n; I ++)
For (j = k + 1; j <= n + 1; j ++)
A [I] [j] = a [I] [j]-(a [I] [k]) * (a [k] [j]) /(a [k] [k]);
}
Res [n] = (a [n] [n + 1])/(a [n] [n]);
Double sum;
For (k = n-1; k> = 1; k --)
{
Sum = 0;
For (j = k + 1; j <= n; j ++)
Sum + = (a [k] [j]) * (Res [j]);
Res [k] = (a [k] [n + 1]-sum)/(a [k] [k]);
}
Cout <"For the follow matrix:" <endl;
Cout <"2x1-1x2-1x3 = 4" <endl;
Cout <"3x1 + 4x2-2x3 = 11" <endl;
Cout <"3x1-2x2 + 4x3 = 11" <endl;
Cout <"The answer is :";
For (I = 1; I <= n; I ++)
Printf ("x [% d] = %. 4f", I, Res [I]);
Cout <endl;
}
Int main ()
{
GussPro ();
Return 0;
}
3. known function table

 

X 1.1275

1.1503

1.1735

1.1972

F (x) 0.1191

0.13954

0.15932

0.17903

 

Calculate the approximate value of f (1.1300) using the Laplace interpolation formula.


# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
# Include <cmath>
# Include <algorithm>
 
Using namespace std;
 
Const int LenOfNum = 4;
Double x [] = {1.1275, 1.1503, 1.1735, 1.1972 };
Double y [] = {0.1191, 0.13954, 0.15932, 0.17903 };
Double Li [LenOfNum];
Int I, j;
Void GetLi (double x0)
{
For (I = 0; I <LenOfNum; I ++)
{
Li [I] = 1;
For (j = 0; j <LenOfNum; j ++)
If (I! = J) Li [I] * = (x0-x [j])/(x [I]-x [j]);
}
}
 
Void ProLag ()
{
Double X 0 = 1.1300;
Double res = 0;
GetLi (x0 );
For (I = 0; I <LenOfNum; I ++)
Res + = (y [I] * Li [I]);
Printf ("f (1.1300) = % f \ n", res );
}
Int main ()
{
ProLag ();
}
# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
# Include <cmath>
# Include <algorithm>

Using namespace std;

Const int LenOfNum = 4;
Double x [] = {1.1275, 1.1503, 1.1735, 1.1972 };
Double y [] = {0.1191, 0.13954, 0.15932, 0.17903 };
Double Li [LenOfNum];
Int I, j;
Void GetLi (double x0)
{
For (I = 0; I <LenOfNum; I ++)
{
Li [I] = 1;
For (j = 0; j <LenOfNum; j ++)
If (I! = J) Li [I] * = (x0-x [j])/(x [I]-x [j]);
}
}

Void ProLag ()
{
Double X 0 = 1.1300;
Double res = 0;
GetLi (x0 );
For (I = 0; I <LenOfNum; I ++)
Res + = (y [I] * Li [I]);
Printf ("f (1.1300) = % f \ n", res );
}
Int main ()
{
ProLag ();
}

From int64Ago's column
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.