这学期新增加了一门课,名为“数值方法”。其实就是利用数学方法结合编程以解决一些实际问题~
看似简单,其实里面也包含了很多数学技巧!班里大部分人都觉得这门课很难,的却对于没怎么掌握好一门编程语言的人而言,要他们用代码体现这一算法,很有挑战性!
我个人觉得很Easy!
最近听到一句话:觉得很中肯.自恋和自负比自卑更好,与其自卑,不如自恋一会!哈哈..也许是谬论,但是也很有借鉴价值。最近被学校的“世纪英才”录取了,心情不错!很久没有写博文了,今天就抽空写下!希望大家能继续关注我....
下学期貌似有VC程序设计,是外教。期待中!
好了,言归正传!
1、迭代函数对收敛性的影响
实验题目1:用迭代法求方程的根。
方案1 化方程为等价方程
取初值x0=1,迭代10次。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//
double G1(double x)
{
return pow((x+1)/2,1.0/3.0);
}
double G2(double x)
{
return 2*x*x*x-1;
}
void Simply_1(double x1,int n)
{
//
double x_new;
cout<<"Text 1"<<endl;
for(int i=0;i<n;i++){
x_new=G1(x1);
cout<<setprecision(7)<<fixed<<x_new<<endl;
x1=x_new;
}
//cout<<x1<<endl;
}
void Simply_2(double x2,int n)
{
double x_old;
cout<<"Text 2"<<endl;
for(int j=0;j<n;j++){
x_old=G2(x2);
cout<<setprecision(8)<<fixed<<x_old<<endl;
x2=x_old;
}
//cout<<x2<<endl;
}
int main()
{
//
int n,m;
double x1,x2;
n=10;
m=10;
x1=0.0;
Simply_1(x1,n);
x2=0.0;
Simply_2(x2,m);
return 0;
}
2、高斯列主元方法实例
1)用列主元消元法求解,分析对结果的影响。
,
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void Print(double a[][5])
{
int i,j;
for(i=0;i<4;i++){
for(j=0;j<5;j++){
cout<<a[i][j]<<' ';
}
cout<<endl;
}
cout<<endl;
}
int main()
{
//
double a[4][5]={1,-1,1,-4,2,
5,-4,3,12,4,
2, 1,1,11,3,
2,-1,7,-1,0};//
double b[4][5]={0.3e-15,
int i,j,k,n;
int Line,Row;
double temp[4][5];//中间量
Row=4;Line=5;//Inintial
//
cout<<"最初的矩阵为:"<<endl;
Print(a);
//
//the main process is under
int kk;//flags
double max;
//bool flag=false;
double t;//the temp of change;
for(k=0;k<Row-1;k++){
///
//search the max_num
//flag=false;
max=a[k][k];
kk=k;
for(i=k;i<Row;i++){
if(abs(a[i][k])>max){
max=a[i][k];
kk=i;
//flag=true;
}
}
//change the line
for(j=0;j<Line;j++){
t=a[k][j];
a[k][j]=a[kk][j];
a[kk][j]=t;
}
cout<<"第"<<k+1<<"次换行结果:"<<endl;
Print(a);
cout<<endl;
cout<<"第"<<k+1<<"次消元结果:"<<endl;
//消元的过程
for(i=k+1;i<Row;i++){
//
temp[i][k]=a[i][k]/a[k][k];
for(j=k;j<Line;j++){
a[i][j]-=temp[i][k]*a[k][j];
}
}
///
Print(a);
//
}
//回带的过程
n=Row-1;
for(i=n;i>=0;i--){
//
double temp_new;
temp_new=0;
for(j=i+1;j<=n;j++){
a[i][n+1]-=a[i][j]*a[j][n+1];
}
a[i][n+1]/=a[i][i];
}
/
cout<<"经过消元后的增广矩阵为:"<<endl;
Print(a);
//
//Print;
cout<<"最后的解为:"<<endl;
for(i=0;i<Row;i++){
//
cout<<"X"<<i+1<<"="<<a[i][Row]<<endl;
}
/
return 0;
}