Linear Equations Problem involving Matrices

1 visualizzazione (ultimi 30 giorni)
Claire
Claire il 12 Feb 2015
Commentato: Claire il 12 Feb 2015
I'm having a bit of difficulty getting my code to work. The problem I need it for is as follows:
In a Cartesian Coordinate system the equation of a a circle with its center at point (a,b) a radius r is:
(x-a)^2+(y-b)^2=r^2
Given three points, (-1, 3.2), (-8,4), and (-6.5, -9.3), determine the equation of the circle that passes through the points. Solve the problem by deriving a system of three linear equations (substitute the points into the equation and solve the system. (a) Use the user-defined function GaussPivot (provided below). (b) Solve the system of equations using MATLAB's left division operation.
For both part a and b I seem to be having trouble with my matrix dimensions, because I keep receiving this error: Attempted to access ab(3,3); index out of bounds because size(ab)=[3,2]. Also, it says that I have an error in this line of GaussPivot: x® = ab (R,C)/ab(R,R); I'm not completely sure what to do though.
Here is the GaussPivot code:
function x = GaussPivot(a,b)
%
% The function solves a system of linear equations ax = b using the Gauss
% elimination method with pivoting.
%
% Input variables:
% a - The matrix of coefficients.
% b - Right-hand-side column vector of constants.
%
% Output variable:
% x - A column vector with the solution.
%
%
% ab - augmented matrix
ab = [a,b];
% R - # of rows C - # of columns in ab
[R, C] = size(ab);
%
for j = 1:R - 1
% Pivoting section starts
if ab(j,j) == 0 % Check if the pivot element is identically zero
for k = j + 1:R
if ab (k,j) ~= 0 % if the off-diagonal terms are not equal to zero
abTemp = ab(j,:); %
ab(j,:) = ab(k,:);
ab(k,:) = abTemp;
break
end
end
end
% Pivoting section ends
for i = j + 1:R
ab(i,j:C) = ab(i,j:C) - ab (i,j)/ab(j,j)*ab(j,j:C);
end
end
x = zeros(R,1);
x(R) = ab (R,C)/ab(R,R);
for i = R - 1:-1:1
x(i) = (ab(i,C) - ab(i,i + 1:R)*x(i + 1:R))/ab(i,i);
end
I then made a separate m-file for part a.
% x and y coordinates
x=[-1 -8 -6.5]
y=[3.2 4 -9.3]
% Matrices with the equations
A=[-2*x(1)-2*y(1);-2*x(2)-2*y(2);-2*x(3)-2*y(3)];
B=[-(x(1)^2+y(1)^2);-(x(2)^2+y(2)^2);-(x(3)^2+y(3)^2)];
%Solve for unknowns
C=GaussPivot(A,B)
a=C(1)
b=C(2)
r=sqrt(C(1)^2+C(2)^2-C(3)^2)
This is where I get my errors. Any help is greatly appreciated. Thank you!
  2 Commenti
Torsten
Torsten il 12 Feb 2015
As far as I can see, your matrix A is (3x1), but it has to be (3x3).
Best wishes
Torsten.
Claire
Claire il 12 Feb 2015
Thank you! That helped a lot!

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Systems Of Linear Equations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by