【数值分析】数值分析部分算法和代码

参考书:Numerical Analysis (9th Edition) R L Burder & J D Faires
代码没用Matlab写,用的是Online Octave免费的,google搜一下Octave就能找到。

Chapter 2 Solutions of Equations int One Variable

一元方程求解

Algorithm 2.1 The Bisection Method (二分法)

在这里插入图片描述

Code

clc; clear;close all;
% LEI YUXIN  Blog:https://blog.csdn.net/Cynthia018
function val = func(x)
    val = x - 2*sin(x);
end

tol = 1.e-5;
a = 1.0;
b = 2.0;
nmax = 100;
% Initialization
iteration = 0;
error = 1.0;

% iteration begins here
fprintf('%-10s %-10s %-10s %-10s %-10s %-8s %s\n','iteration','a','b','p','fa','fp','error');
while (iteration <= nmax && error >= tol)
    iteration++; % Generate and save iteratres
    p = a + (b - a) / 2;
    z(iteration) = p;
    fa = func(a);
    fp = func(p);
    error = abs(a-p)/(abs(p) + eps);
    fprintf('%5d %10.5f %10.5f %10.5f %10.6f %10.7f %10.6f\n',iteration,a,b,p,fa,fp,error);
    if (error < tol)
        p_final = p;
    elseif (fa*fp < 0) % root is between a and p
        b = p;
    else    %root is between p and b
        a = p;
    end
end

Algorithm 2.2 Fixed-Point Iteration (不动点迭代)

请添加图片描述

Code

clc;clear;close all;
% LEI YUXIN  Blog:https://blog.csdn.net/Cynthia018
function val=g(x)
    val=(2-exp(x)+x^2)/3;
end

tol = 1.e-5;
nmax = 100;
%initialization
p = [0]; % initialized p0
count = 1;  %iteration = count-1
abs_error = [1.0];

while (count <= nmax && abs_error(count) >= tol)
    p(count+1) = g(p(count));
    abs_error(count+1) = abs(p(count+1)-p(count));
    count ++; % Generate and save count
end

if ( count-1 < nmax)
    %print table
    printf(" n |\tpn\t| abs.error |\n");
    printf("----------------------------------\n");
    for i=1:count
        printf(" %d | %.7f\t| %-10.6f|\n",i-1,p(i),abs_error(i));
    end
    printf("The number of iterations is %d", count-1); 
else
    printf("Exceeded maximum iterations");
end

Algorithm 2.3 Newton’s Method (Newton迭代法)

请添加图片描述

Code

clc;clear;close all;
% LEI YUXIN  Blog:https://blog.csdn.net/Cynthia018
function val=f(x)
    val=cos(x)-x;
end
function dval=df(x)
    dval=-sin(x)-1;
    % dval=diff(g,x);
end

tol = 1.e-5;
nmax = 100;
%initialization
p = [1]; % initialized p0
count = 1;  %iteration = count-1
abs_error = [1.0];

while (count <= nmax && abs_error(count) >= tol)
    p(count+1) = (p(count) - f(p(count))/df(p(count)));
    abs_error(count+1) = abs(p(count+1)-p(count));
    count ++; % Generate and save count
end

if ( count-1 < nmax)
    % print table
    printf(" n | p(n-1)\t| pn\t\t| pn-p(n-1)\t| abs.error |\n");
    printf("----------------------------------\n");
    for i=2:count
        printf(" %d | %.7f\t| %.7f\t| %.7f\t| %-10.6f|\n",i-1,p(i-1),p(i),p(i)-p(i-1),abs_error(i));
    end
    printf("The number of iterations is %d", count-1); 
else
    printf("Exceeded maximum iterations");
end

Algorithm 2.4 Secant (正割法)

请添加图片描述

Code

clc;clear;close all;
% LEI YUXIN  Blog:https://blog.csdn.net/Cynthia018
function val=f(x)
    val=cos(x)-x;
end

tol = 1.e-5;
nmax = 100;
abs_error(2) = [1.0];
count = 2;
p = [0.5];

q0 = f(p(1));
p(2) = pi/4;
q1 = f(p(2));

while (count < nmax && abs_error(count) >= tol)
    p(count+1) = p(count) - q1*(p(count)-p(count-1))/(q1-q0);
    abs_error(count+1) = abs(p(count+1)-p(count));
    count++;
    q0 = q1;
    q1 = f(p(count));
end

if ( count-1 < nmax)
    %print table
    printf(" n |p(n-2)\t|p(n-1)\t\t|pn\t\t| abs.error |\n");
    printf("----------------------------------\n");
    for n=3:count
        printf(" %d | %.7f\t| %.7f\t| %.7f\t| %-10.6f|\n",n-1,p(n-2),p(n-1),p(n),abs_error(n));
    end
    printf("The number of iterations is %d", count-1); 
else
    printf("Exceeded maximum iterations");
end

Algorithm 2.5 False Position (试位法)

请添加图片描述

Code

clc;clear;close all;
% LEI YUXIN  Blog:https://blog.csdn.net/Cynthia018
function val=f(x)
    val=cos(x)-x;
end

tol = 1.e-5;
nmax = 100;
abs_error(2) = [1.0];
count = 2;
p = [0.5];

q0 = f(p(1));
p(2) = pi/4;
q1 = f(p(2));
p0 = p(1);
p1 = p(2);

while (count < nmax && abs_error(count) >= tol)
    p(count+1) = p1 - q1*(p1-p0)/(q1-q0);
    abs_error(count+1) = abs(p(count+1)-p1);
    count++;
    q = f(p(count));
    if (q*q1 < 0)
        p0 = p1;
        q0 = q1;
    end
    p1 = p(count);
    q1 = q;
end

if ( count-1 < nmax)
    %print table
    printf(" n |p(n-2)\t|p(n-1)\t\t|pn\t\t| abs.error |\n");
    printf("----------------------------------\n");
    for n=3:count
        printf(" %d | %.7f\t| %.7f\t| %.7f\t| %-10.6f|\n",n-1,p(n-2),p(n-1),p(n),abs_error(n));
    end
    printf("The number of iterations is %d", count-1); 
else
    printf("Exceeded maximum iterations");
end

Chapter 3 Lagrange Interpolating Polynomials

Algorithm 3.1 Neville’s Iterated Interpolation (Neville迭代插值法)

请添加图片描述

Algorithm 3.3 Neville’s Interpolation (Neville插值)

请添加图片描述

Code

clc;clear;close all;
% LEI YUXIN  Blog:https://blog.csdn.net/Cynthia018
%initialization
x=[0.32, 0.35]; %x0,x1,...,xn
f=[0.3146, 0.3429]; %f(x)
f2=[0.9492, 0.9394]; %f'(x)

z=[];
Q=[];
%initialize z and Q arrays
for i=1:size(x,2)
    %duplicate x
    z(2*i-1)=x(i);
    z(2*i)=x(i);
    
    %duplicate f(x)
    Q(2*i-1,1)=f(i);
    Q(2*i,1)=f(i);
end

%starting with j=2 as first column is already initialized
for j=2:size(z,2) %size of array z in 2nd dimension (number of columns)
    for i=j:size(z,2)
        %if i is an even number, and j is 2 (2nd column)
        %is where z0=z1, hence use derivative of f(x)
        if (mod(i,2)==0 && j==2)
            Q(i,j)=f2(i/2);
        else
            Q(i,j)=(Q(i,j-1)-Q(i-1,j-1))/(z(i)-z(i-j+1));
        end
   end
end

%display Q
printf("Array of Q:\n");
for i=1:size(Q,1)
    for j=1:size(Q,2)
        printf("%10f  ",Q(i,j));
    end
    printf("\n");
end

Algorithm 3.5 Natural Cubic Spline (自然三次样条)

请添加图片描述

Chapter 4 Numerical Differentiation

Algorithm 4.1 Composite Integration: Simpson’s Rule Algorithm (复合Simpson法则)

请添加图片描述

Code

clc; clear;close all;
% LEI YUXIN  Blog:https://blog.csdn.net/Cynthia018
function val=f(x)
    val=exp(x);
end

a=0;
b=4;
%h=2;
h=1;
n=(b-a)/h;

XI0=f(a)+f(b);
XI1=0;
XI2=0;

for i=1:n-1
    X=a+i*h;
    if(mod(i,2)==0)
        XI2=XI2+f(X);
    else
        XI1=XI1+f(X);
    end
end
XI=h*(XI0+2*XI2+4*XI1)/3;
printf("If ");h
printf("XI=%f",XI);

Algorithm 4.2 Romberg

请添加图片描述
请添加图片描述

Code

clc; clear;close all;
% LEI YUXIN  Blog:https://blog.csdn.net/Cynthia018
function val=f(x)
    val=sin(x);
end

a=0;
b=pi;
h=b-a;
n=6;
R=[];
%R(1,1)=h/2*(f(a)+f(b));
R(1,1)=0;
printf("%.8f\n",R(1,1));
%step3
for i=2:n
    %step4
    sum=0;
    for k=1:2^(i-2)
        sum=sum+f(a+(k-0.5)*h);
    end
    R(2,1)=1/2*(R(1,1)+h*sum);
    printf("%.8f\t",R(2,1));
    %step5
    for j=2:i
        R(2,j)=R(2,j-1)+(R(2,j-1)-R(1,j-1))/(4^(j-1)-1);
        printf("%.8f\t",R(2,j));
    end
    printf("\n");
    %step7
    h/=2;
    for j=1:i
        R(1,j)=R(2,j);
    end
end

Chapter 5 Elementary Theory of Initial-Value Problems

Algorithm 5.1 Euler’s Method

请添加图片描述
请添加图片描述

Code

clc; clear;close all;
% LEI YUXIN  Blog:https://blog.csdn.net/Cynthia018
a=0;
b=2;
h=0.5;
N=(b-a)/h;
t=a;
alpha=0.5;
w=alpha;

printf("t\t\tw\n");
printf("%f\t%f\n",t,w);

for i=1:N
    w=w+h*(w-((i-1)*h)^2+1);
    t=a+i*h;
    printf("%f\t%f\n",t,w);
end
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值