Main Content

fixed.qrMatrixSolve

Solve system of linear equationsAx=Bforxusing QR decomposition

Description

example

x= fixed.qrMatrixSolve(A,B)solves the system of linear equationsAx=Busing QR decomposition.

example

x= fixed.qrMatrixSolve(A,B,outputType)returns the solution to the system of linear equationsAx=Bas a variable with the output type specified byoutputType.

example

x= fixed.qrMatrixSolve(A,B,outputType,regularizationParameter)returns the solution to the system of linear equations

[ λ I n A ] x = [ 0 n , p B ]

whereAis anm-by-nmatrix,Bis anm-by-pmatrix, and λ is the regularization parameter.

Examples

collapse all

This example shows how to solve a simple system of linear equations Ax = b , using QR decomposition.

In this example, define A as a 5-by-3 matrix with a large condition number. To solve a system of linear equations involving ill-conditioned (large condition number) non-square matrices, you must use QR decomposition.

rngdefault; A = gallery('randsvd', [5,3], 1000000); b = [1; 1; 1; 1; 1]; x = fixed.qrMatrixSolve(A,b)
x =3×1104× -2.3777 7.0686 -2.2703

Compare the result of thefixed.qrMatrixSolvefunction with the result of themldivideor\function.

x = A\b
x =3×1104× -2.3777 7.0686 -2.2703

This example shows the effect of a regularization parameter when solving an overdetermined system. In this example, a quantityyis measured at several different values of timetto produce the following observations.

t = [0 .3 .8 1.1 1.6 2.3]'; y = [.82 .72 .63 .60 .55 .50]';

Model the data with a decaying exponential function

y ( t ) = c 1 + c 2 e - t .

The preceding equation says that the vectoryshould be approximated by a linear combination of two other vectors. One is a constant vector containing all ones and the other is the vector with componentsexp(-t). The unknown coefficients, c 1 and c 2 , can be computed by doing a least-squares fit, which minimizes the sum of the squares of the deviations of the data from the model. There are six equations and two unknowns, represented by a 6-by-2 matrix.

E = [ones(size(t)) exp(-t)]
E =6×21.0000 1.0000 1.0000 0.7408 1.0000 0.4493 1.0000 0.3329 1.0000 0.2019 1.0000 0.1003

Use thefixed.qrMatrixSolvefunction to get the least-squares solution.

c = fixed.qrMatrixSolve(E, y)
c =2×10.4760 0.3413

In other words, the least-squares fit to the data is

y ( t ) = 0 . 4 7 6 0 + 0 . 3 4 1 3 e - t .

The following statements evaluate the model at regularly spaced increments int, and then plot the result together with the original data:

T = (0:0.1:2.5)'; Y = [ones(size(T)) exp(-T)]*c; plot(T,Y,“- - -”,t,y,'o')

Figure contains an axes object. The axes object contains 2 objects of type line.

In cases where the input matrices are ill-conditioned, small positive values of a regularization parameter can improve the conditioning of the least squares problem, and reduce the variance of the estimates. Explore the effect of the regularization parameter on the least squares solution for this data.

figure; lambda = [0:0.1:0.5]; plot(t,y,'o','DisplayName','Original Data');fori = 1:长度(λ)c =fixed.qrMatrixSolve(E, y, numerictype('double'), lambda(i)); Y = [ones(size(T)) exp(-T)]*c; holdonplot(T,Y,“- - -”,'DisplayName', ['lambda =', num2str(lambda(i))])endlegend('Original Data','lambda = 0','lambda = 0.1','lambda = 0.2','lambda = 0.3','lambda = 0.4','lambda = 0.5')

Figure contains an axes object. The axes object contains 7 objects of type line. These objects represent Original Data, lambda = 0, lambda = 0.1, lambda = 0.2, lambda = 0.3, lambda = 0.4, lambda = 0.5.

Input Arguments

collapse all

Coefficient matrix in the linear system of equationsAx=B.

Data Types:single|double|fi
Complex Number Support:Yes

Input vector or matrix representingBin the linear system of equationsAx=B.

Data Types:single|double|fi
Complex Number Support:Yes

Output data type, specified as anumerictypeobject or a numeric variable. IfoutputTypeis specified as anumerictypeobject, the output,x, will have the specified data type. IfoutputTypeis specified as a numeric variable,xwill have the same data type as the numeric variable.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|fi|numerictype

Regularization parameter, specified as a non-negative scalar. Small, positive values of the regularization parameter can improve the conditioning of the problem and reduce the variance of the estimates. While biased, the reduced variance of the estimate often results in a smaller mean squared error when compared to least-squares estimates.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|fi

Output Arguments

collapse all

Solution, returned as a vector or matrix. IfAis anm-by-nmatrix andBis anm-by-pmatrix, thenxis ann-by-pmatrix.

Extended Capabilities

Introduced in R2020b