lsm_fun.m

来自「最小二乘法(Least-square method)在matlab平台下的实现程」· M 代码 · 共 29 行

M
29
字号
function [Pfit_inv]=lsm_fun(x,y,n)
% solve LSM problem by calculate P0=inv(A)*b
% the function returns coefficients in decrease
% version: lsm v0.2
[temp,N]=size(x);   % N: elements number
highorder=n;    % the highest order of the polynomial
P0=zeros(highorder+1,1);            % increase by row
Pfit_inv=zeros(1,highorder+1);      % decrease by colum
A=zeros(highorder+1,highorder+1);   % A*P0=b
b=zeros(highorder+1,1);
for row0=0:highorder     % 计算法方程
    for colum0=row0:highorder
        row=row0+1;colum=colum0+1;  % for compilers that index from 1 not 0
%       row=row0;  colum=colum0;    % for compilers that index from 0 not 1
        for i0=0:(N-1)
            ii=i0+1;
%           ii=i0;
            A(row,colum)=A(row,colum)+power(x(ii),colum0)*power(x(ii),row0);
        end
        A(colum,row)=A(row,colum);
    end
    for i0=0:(N-1)
        ii=i0+1;
%       ii=i0;
        b(row)=b(row)+y(ii)*power(x(ii),row0);
    end
end
P0=inv(A)*b;    % LU deco
Pfit_inv=P0(highorder+1:-1:1)';

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?