Here is the function:
function [X1,X2,X] = msystem(A,B)
[~,n] = size(A);
[~,p] = size(B);
[L,U] = lu(A);
%solving for X1
X1 = inv(A)*B;
invL = [L eye(n)];
invL = rref(invL);
invL = invL(:,(n+1:n*2));
Y = invL*B;
invU = [U eye(n)];
invU = rref(invU);
invU = invU(:,(n+1:n*2));
X2 = invU * Y;
X = Y ./ U;
if (closetozeroroundoff(X1 - X2) == zeros(n,p))
disp('The solutions are the same')
else
disp('An error has been made on this exercise')
end
if (closetozeroroundoff(X1 - X) == zeros(n,p))
disp('The solutions are the same')
else
disp('An error has been made on this exercise')
end
end

 채택된 답변

Adam Danz
Adam Danz 2021년 3월 18일
편집: Adam Danz 2021년 3월 18일

0 개 추천

closetozeroroundoff(), a 3rd party function not defined in the question, contains more than one input and one of the inputs is apparently named H or p. However, you're only providing 1 input so H or p is not defined in the function.

댓글 수: 11

Tianlan Yang
Tianlan Yang 2021년 3월 18일
Could you please show me how to edit it?
I can't because I don't know what closetozeroroundoff is, I don't know how many inputs there are, and I don't know what the inputs should be.
In this line,
if (closetozeroroundoff(X1 - X2) == zeros(n,p))
you are only providing one input to the function. That's not enough inputs. So, open the file containing closetozeroroundoff and look at how many inputs there should be in the first line of code. If you're lucky, the file may contain documentation that explains what the inputs should be.
Tianlan Yang
Tianlan Yang 2021년 3월 18일
I try to use if (closetozeroroundoff(X1 - X2,7) == zeros(n,p)), solve this problem, but there is another error, how to print out the different dimension matrix?
I encourage you to learn how to use debug mode so you can independently solve these kinds of problems.
The error message tells you the function and line number that fails. You can put a break point on that line (see link above) and then look at the size of Y and U using
size(Y)
size(U)
You'll see that they have different sizes.
The error message tells you that their dimensions must agree for that operation.
Note that the sizes of A and B match the sizes of Y and U so, without looking at the code, I'd bet that the sizes of A and B should also be the same. I bet this would not throw any errors,
A = magic(5); B = randi(10,5,5)
but most importantly, is that what the inputs should be and is the code doing what you intend it to do? Those are questions you need to answer as the developer of the program.
Tianlan Yang
Tianlan Yang 2021년 3월 18일
Thank you so much! I think I understand what you mean. I have another similar question but with different error, could you please help me to see this? Thank you so much!
Here is the function:
function [L,U] = eluinv(A)
[~,n]=size(A);
[L,U] = lu(A)
LU = roundingfixer(L*U);
if (A == LU)
disp('LU factorization is valid')
else
disp('A mistake was made somewhere.')
end
if (rank(U) == rank(A))
disp('U is an echelon form of A')
else
disp('U is not an echelon form of A? What is wrong?!')
end
if (rank(A) == n)
invL = [L eye(n)];
invU = [U eye(n)];
invL = rref(invL);
invU = rref(invU);
invL = invL(:,(n+1:n*2));
invU = invU(:,(n+1:n*2));
invA = roundingfixer(invU * invL)
P = roundingfixer(inv(A))
disp('the inverse of A calculated using LU factorization is')
disp(invA)
if (invA == P)
disp('Yes, LU factorization works for calculating the inverses')
else
disp('LU factorization does not work for me!?')
end
else
sprintf('A is not invertible')
invA = [];
end
end
Adam Danz
Adam Danz 2021년 3월 18일
Same comment as above.
The error message tells you which line is the problem.
That line is trying to horizontally concatenate two variables but one variable is 4x3 and the other is 3x3 but horizontal concatenation requires the same number of rows.
Tianlan Yang
Tianlan Yang 2021년 3월 18일
Yes, I got your idea. I want to know if they have different number of rows, how to adjust it to make it run.(Maybe a function that can replace horizontally concatenate?)
Adam Danz
Adam Danz 2021년 3월 19일
It looks like A needs to be a square matrix.
Tianlan Yang
Tianlan Yang 2021년 3월 19일
Can you show me how to convert A the 3*4 trasnpose to a square matrix?
Tianlan Yang
Tianlan Yang 2021년 3월 19일
For example, if a matrix is 4*3 [1 2 3; 4 5 6; 7 8 9; 10 11 12], how to make it look like 4*4?
Adam Danz
Adam Danz 2021년 3월 19일
편집: Adam Danz 2021년 3월 19일
That's like asking how to make this cube look like a circle. A 4x3 matrix can never look like a 3x3 matrix without changing the data. You can remove the first row, second row, third row, or 4th row of the 4x3 matrix but now it's a completely different variable.
m = (1:4)'.*ones(1,3)
m = 4×3
1 1 1 2 2 2 3 3 3 4 4 4
m(3,:) = [] % remove 3rd row
m = 3×3
1 1 1 2 2 2 4 4 4

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2021년 3월 18일

편집:

2021년 3월 19일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by