参考书:Numerical Analysis (9th Edition) R L Burder & J D Faires
代码没用Matlab写,用的是Online Octave免费的,google搜一下Octave就能找到。
一元方程求解
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
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
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
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
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
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
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);
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
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