Matlab实现——Upper Triangularization Followed by Back Subtitution

欢迎前往个人博客 驽马点滴 和视频空间 哔哩哔哩-《挨踢日志》

 

uptrbk.m

 

 

%Program 3.2  (Uppr Triangularization Followed by Back Subtitution)

function X = uptrbk(A,B)

%Input  - A is an N x N nonsingular matrix

%        - B is an N x 1 matrix

%Output - X is an N x 1 matrix containing the solution to AX=B.

%Initialize X and the temporary storage matrix C 

[N N]=size(A);

X=zeros(N,1);

C=zeros(1,N+1);

%Form the augmented matrix: Aug=[A|B]

Aug=[A B];

for p=1:N-1

     %Partial pivoting for column p

     [Y,j]=max(abs(Aug(p:N,p)));

     %Interchange row p and j

     C=Aug(p,:);

     Aug(p,:)=Aug(j+p-1,:);

     Aug(j+p-1,:)=C;

  if Aug(p,p)==0

      'A was singular.  No unique solution'

     break

  end

  %Elimination process for column p

  for k=p+1:N

     m=Aug(k,p)/Aug(p,p);

     Aug(k,p:N+1)=Aug(k,p:N+1)-m*Aug(p,p:N+1);

  end

end

 

%Back Substitution on [U|Y] using Program 3.1

X=backsub(Aug(1:N,1:N),Aug(1:N,N+1));
 

 

 

Backsub.m

 

function X=backsub(A,B)

 

%Input  - A is an n x n upper-triangular nonsingular  matrix

%        - B is an n x 1 matrix

%Output - X is the solution to the linear system AX = B

%Find the dimension of B and initialize X

n=length(B);

 X=zeros(n,1);

 X(n)=B(n)/A(n,n);

for k=n-1:-1:1

      X(k)=(B(k)-A(k,k+1:n)*X(k+1:n))/A(k,k);

end
 

 


 Untitle.m

 

A=[

    1 0 0 0 0 0 0

    1 1 1 1 1 1 1

    1 2 4 8 16 32 64

    1 3 9 27 81 243 729

    1 4 16 64 256 1024 4096

    1 5 25 125 625 3125 15625

    1 6 6^2 6^3 6^4 6^5 6^6];

B=[1;3;2;1;3;2;1];

X = uptrbk(A,B)
 

 

欢迎前往个人博客 驽马点滴 和视频空间 哔哩哔哩-《挨踢日志》

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
L21范数是一种用于矩阵优化的正则化项,常用于处理高维数据下的稀疏表示问题。它结合了L2范数和平方核化的性质,在机器学习、信号处理和统计学等领域有着广泛的应用。 在MATLAB中,实现L21范数的基本思路是通过最小化含有该正则项的目标函数。目标函数通常包含损失函数和正则化项两部分,其中正则化项就是L21范数。L21范数对于一个n x p的矩阵A来说,可以看作是对每一列应用L2范数然后对所有列求L1范数的结果。即: \[ \text{L21范数} = \sum_{i=1}^{p} \sqrt{\sum_{j=1}^{n} A_{ij}^2} \] 在实际计算中,为了方便操作,我们经常将矩阵A转换成一维向量,然后计算其绝对值的一维向量的L1范数,再乘以根号下矩阵元素的数量(n * p),这样可以避免直接逐行累加和开方的操作。以下是具体的MATLAB代码示例: ```matlab function l21_norm = calculate_l21(A) % Calculate the L21 norm of a matrix A. % Input: Matrix A (n x p) as a single input argument % Output: The L21 norm as scalar % Convert matrix to column vector and take absolute values vec_A = abs(A(:)); % Compute L21 norm as sum of squared elements followed by square root, % then multiply back by sqrt(n*p), where n is number of rows in A, p is columns. l21_norm = sqrt(sum(vec_A.^2)) * sqrt(size(A,1)*size(A,2)); end ``` ### 示例如何使用此函数: 假设有一个随机生成的矩阵 `M`: ```matlab M = rand(5, 4); % Generate a 5x4 random matrix l21_result = calculate_l21(M); disp(l21_result); ``` ### 相关问题: 1. **解释L21范数在稀疏表示中的作用** - 说明为什么在处理稀疏数据或者特征选择时,L21范数特别有用。 2. **比较L21范数与其他正则化项(如L1、L2等)的区别** - 给出几个典型的例子,展示如何在实际的数据分析和建模过程中应用L21范数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值