is there anything wrong with my function? MATRIX
显示 更早的评论
I am currently doing this question and my function in MATLAB won't execute due to the fact that I'm not entering a display function.
Then I did and the function is still not working
The question states
Read in the coefficient matrix, A, and constant vector, b (see below).
- Check that A is square and that A and b are compatible sizes, and take appropriate action if they aren’t. (The size() function will be handy here.)
- Calculate and display the determinant of A. (Use the det() function.)
- Solve for the vector of unknowns, x, both by multiplying by the inverse of the coefficient matrix, and by using the backslash operator. Display both results.
- Time how long it takes to calculate each of these solutions. This is easy: call the tic function just before the section of code you want to time and the toc function just after it. The elapsed time will be displayed in the command window.
- Test the accuracy of each solution. The best way of doing this is to calculate the residual vector, bAxv−=. You then need a measure of how ‘big’ v is. There are a few possibilities:
a. Maximum absolute value: max(abs(v))
b. Euclidian norm: norm(v). (This is the square root of the sum of the squares of the vector elements)
and these are the rules I followed
my function is below
function SolvingSimEquations()
%this function will solve sets of equations where the number of equations
%equals the number of unknowns
%A MacLean-Bell
%29/04/16
%the function must read in the coefficient matrix, A
%and constant vector, b
MatrixA = input('Enter matrix A');
VectorB= input('Enter Vector B');
[nrowA,ncolA] = size(MatrixA);
[nrowB,~] = size(VectorB);
if nrowA == ncolA
if nrowA == nrowB
D = det (MatrixA);
disp(['The determinant is; ' num2str(D)])
tic %time starts
x1 = MatrixA\VectorB;
toc
disp (['The time it took for the function is; ' num2str(x1)])
tic
x2 = inv(MatrixA)*VectorB;
toc %time stops
disp (['the time it took for the function x2 is; ' numstr(x2)])
v1 = MatrixA(x1)-VectorB;
v2 = MatrixA(x2)-VectorB;
n1 = norm(v1);
n2 = norm(v2);
disp (['the answer to n1 is; ' numstr(n1)])
disp (['the answer to n2 is; ' numstr(n2)])
end
end
end
if I could get an answer for this it would be great
thanks so much
回答(2 个)
KSSV
2016-4-29
function SolvingSimEquations()
%this function will solve sets of equations where the number of equations
%equals the number of unknowns
%A MacLean-Bell
%29/04/16
%the function must read in the coefficient matrix, A
%and constant vector, b
MatrixA = input('Enter matrix A');
VectorB= input('Enter Vector B');
% Check that A is square and that A and b are compatible sizes,
[nrowA,ncolA] = size(MatrixA);
if nrowA ~= ncolA
error('Matrix A is not a square') ;
end
[nrowB,ncolB] = size(VectorB);
if ncolB ~= 1
error('VectorB is not a vector') ;
elseif nrowA ~= nrowB
error('Matrix A and Vector B are not compatible')
end
% Calculate and display the determinant of A
D = det (MatrixA);
fprintf('The determinant of the matrix A is %f\n',D) ;
%Solve for the vector of unknowns, x, both by multiplying by the inverse of the coefficient matrix,
% and by using the backslash operator. Display both results.
t1 = tic; %time starts
x1 = MatrixA\VectorB;
t1 = toc(t1) ;
fprintf('The time it took for the function x1 is: %f\n',t1)
t2 = tic ;
x2 = inv(MatrixA)*VectorB;
t2 = toc(t2); %time stops
fprintf('the time it took for the function x2 is: %f\n',t2)
v1 = MatrixA*x1-VectorB;
v2 = MatrixA*x2-VectorB;
n1 = norm(v1);
n2 = norm(v2);
fprintf('the time it took for the function x2 is: %f\n',t2)
fprintf('the answer to n1 is: %f\n',n1)
fprintf('the answer to n2 is: %f\n',n2)
end
Roger Stafford
2016-4-29
编辑:Roger Stafford
2016-4-29
You've written the code for v1 and v2 incorrectly. It should be:
v1 = MatrixA*x1-VectorB;
v2 = MatrixA*x2-VectorB;
(It looks as though whoever prepared this problem got their symbols scrambled with "bAxv−=". It presumably should read "v=Ax-b".)
Your testing should be changed so as to "take appropriate action if they aren’t" which means if any of the tests fail, you should display an appropriate (sarcastic?) error message and then quit without continuing the computation.
Also you left out a test for b being a vector with only one column.
Also I think you were supposed to find max(abs(v)) for each vector.
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!