Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Cramer's Rule - linear equations

Subject: Cramer's Rule - linear equations

From: Ashwini Deshpande

Date: 13 May, 2008 06:50:18

Message: 1 of 8

I have three linear equations with 3 unknowns, say for example:

3x + 4y + 6z = 1;
x - 2y + 7z = 10;
2x + 3y - 9z = 15;

How do i solve this using matlab, is there any easiest way
to apply Cramer's Rule to Solve these equations ???

Any help would be appreciated !!
Thanks !
Ashwini

Subject: Re: Cramer's Rule - linear equations

From: NZTideMan

Date: 13 May, 2008 08:06:33

Message: 2 of 8

On May 13, 6:50=A0pm, "Ashwini Deshpande" <vd.ashw...@mathworks.com>
wrote:
> I have three linear equations with 3 unknowns, say for example:
>
> 3x + 4y + 6z =3D 1;
> x - 2y + 7z =3D 10;
> 2x + 3y - 9z =3D 15;
>
> How do i solve this using matlab, is there any easiest way
> to apply Cramer's Rule to Solve these equations ???
>
> Any help would be appreciated !!
> Thanks !
> Ashwini

If you're using Matlab, why would you even contemplate using Cramer's
Rule?
You would only use that if you wanted to solve it by hand - or you
needed to do it for homework.

If you genuinely want to solve the equations in Matlab, try: help
mldivide

Subject: Re: Cramer's Rule - linear equations

From: James Tursa

Date: 13 May, 2008 08:26:04

Message: 3 of 8

"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in
message <g0bdna$rjm$1@fred.mathworks.com>...
> I have three linear equations with 3 unknowns, say for
example:
>
> 3x + 4y + 6z = 1;
> x - 2y + 7z = 10;
> 2x + 3y - 9z = 15;
>
> How do i solve this using matlab, is there any easiest way
> to apply Cramer's Rule to Solve these equations ???
>
> Any help would be appreciated !!
> Thanks !
> Ashwini
>

Cramer's Rule: It may be slow, but it's inaccurate!

James Tursa

Subject: Re: Cramer's Rule - linear equations

From: Dave Bell

Date: 14 May, 2008 00:00:33

Message: 4 of 8

NZTideMan wrote:
> On May 13, 6:50 pm, "Ashwini Deshpande" <vd.ashw...@mathworks.com>
> wrote:
>> I have three linear equations with 3 unknowns, say for example:
>>
>> 3x + 4y + 6z = 1;
>> x - 2y + 7z = 10;
>> 2x + 3y - 9z = 15;
>>
>> How do i solve this using matlab, is there any easiest way
>> to apply Cramer's Rule to Solve these equations ???
>>
>> Any help would be appreciated !!
>> Thanks !
>> Ashwini
>
> If you're using Matlab, why would you even contemplate using Cramer's
> Rule?
> You would only use that if you wanted to solve it by hand - or you
> needed to do it for homework.
>
> If you genuinely want to solve the equations in Matlab, try: help
> mldivide

Perhaps the OP meant "Is there any easier way than to apply Cramer's Rule?"

Subject: Re: Cramer's Rule - linear equations

From: Roger Stafford

Date: 14 May, 2008 00:54:01

Message: 5 of 8

"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message
<g0bdna$rjm$1@fred.mathworks.com>...
> I have three linear equations with 3 unknowns, say for example:
>
> 3x + 4y + 6z = 1;
> x - 2y + 7z = 10;
> 2x + 3y - 9z = 15;
>
> How do i solve this using matlab, is there any easiest way
> to apply Cramer's Rule to Solve these equations ???
>
> Any help would be appreciated !!
> Thanks !
> Ashwini
-------------
  It is true that using Cramer's Rule in matlab would in general constitute an
inefficient method of solving linear equations, when there are so many
superior algorithms available. However, it must be said in defense of
Cramer's Rule that it remains a very useful tool in mathematics, both in
understanding the theory in linear algebra and also for doing certain symbolic
manipulations. I personally have to resort to its use quite often in deriving
various formulas and the like. It is only in the area of actual numerical
computation that other methods become preferable.

Roger Stafford

Subject: Re: Cramer's Rule - linear equations

From: NZTideMan

Date: 14 May, 2008 07:21:45

Message: 6 of 8

On May 14, 12:54=A0pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "Ashwini Deshpande" <vd.ashw...@mathworks.com> wrote in message
>
> <g0bdna$rj...@fred.mathworks.com>...> I have three linear equations with 3=
 unknowns, say for example:
>
> > 3x + 4y + 6z =3D 1;
> > x - 2y + 7z =3D 10;
> > 2x + 3y - 9z =3D 15;
>
> > How do i solve this using matlab, is there any easiest way
> > to apply Cramer's Rule to Solve these equations ???
>
> > Any help would be appreciated !!
> > Thanks !
> > Ashwini
>
> -------------
> =A0 It is true that using Cramer's Rule in matlab would in general constit=
ute an
> inefficient method of solving linear equations, when there are so many
> superior algorithms available. =A0However, it must be said in defense of
> Cramer's Rule that it remains a very useful tool in mathematics, both in
> understanding the theory in linear algebra and also for doing certain symb=
olic
> manipulations. =A0I personally have to resort to its use quite often in de=
riving
> various formulas and the like. =A0It is only in the area of actual numeric=
al
> computation that other methods become preferable.
>
> Roger Stafford

I agree entirely with you Roger, but the OP quoted a numerical
example, not a symbolic one.

How large a matrix would you use Cramer's Rule on? Back when I learnt
it and access to computers was difficult (i.e., Hollerith cards
submitted as a background job) I could handle 3x3 no trouble and 4x4
with a bit of effort, but these days 2x2 would be my limit before
finding a better way.

Subject: Re: Cramer's Rule - linear equations

From: James Tursa

Date: 14 May, 2008 07:53:01

Message: 7 of 8

"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in
message <g0bdna$rjm$1@fred.mathworks.com>...
> I have three linear equations with 3 unknowns, say for
example:
>
> 3x + 4y + 6z = 1;
> x - 2y + 7z = 10;
> 2x + 3y - 9z = 15;
>
> How do i solve this using matlab, is there any easiest way
> to apply Cramer's Rule to Solve these equations ???
>
> Any help would be appreciated !!
> Thanks !
> Ashwini
>

If you want to learn why Cramer's Rule is not a very good
method numerically and why it is avoided, particularly for
large problems, consider the code below and try it for
inputs of 100, 200, 300, 400, 500, 600. The intermediate
numbers get huge (eventually blows up), the timings get very
long, and it is not as accurate as the built in MATLAB \
operator. Like I said, it's slow, but it's inaccurate.

James Tursa

------------------------------

function callcramer(m)
disp(' ');
A = rand(m,m);
b = rand(m,1);
disp('Timing backslash:');
tic
x = A\b; % Backslash operator for solving A*x = b
toc
xc = cramersrule(A,b);
disp(['norm of backslash residual = '
num2str(norm(A*x-b),4)]);
disp(['norm of Cramer''s Rule residual = '
num2str(norm(A*xc-b),4)]);
disp(' ');
return
end

function x = cramersrule(A,b) % Demo of Cramer's Rule for
solving A*x = b
disp('Timing Cramer''s Rule:');
tic
[m n] = size(b);
z = zeros(m,1);
Ai = A;
for k=1:m
    Ai(:,k) = b;
    z(k) = det(Ai);
    Ai(:,k) = A(:,k);
end
detA = det(A);
x = z / detA;
toc
disp(['Max abs(det(Ai)) = ' num2str(max(abs(z)),4)]);
disp(['abs(det(A)) = ' num2str(abs(detA),4)]);
return
end

Subject: Re: Cramer's Rule - linear equations

From: Ashwini Deshpande

Date: 15 May, 2008 05:41:02

Message: 8 of 8

"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in
message <g0e5ot$96$1@fred.mathworks.com>...
> "Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in
> message <g0bdna$rjm$1@fred.mathworks.com>...
> > I have three linear equations with 3 unknowns, say for
> example:
> >
> > 3x + 4y + 6z = 1;
> > x - 2y + 7z = 10;
> > 2x + 3y - 9z = 15;
> >
> > How do i solve this using matlab, is there any easiest way
> > to apply Cramer's Rule to Solve these equations ???
> >
> > Any help would be appreciated !!
> > Thanks !
> > Ashwini
> >
>
> If you want to learn why Cramer's Rule is not a very good
> method numerically and why it is avoided, particularly for
> large problems, consider the code below and try it for
> inputs of 100, 200, 300, 400, 500, 600. The intermediate
> numbers get huge (eventually blows up), the timings get very
> long, and it is not as accurate as the built in MATLAB \
> operator. Like I said, it's slow, but it's inaccurate.
>
> James Tursa
>
> ------------------------------
>
> function callcramer(m)
> disp(' ');
> A = rand(m,m);
> b = rand(m,1);
> disp('Timing backslash:');
> tic
> x = A\b; % Backslash operator for solving A*x = b
> toc
> xc = cramersrule(A,b);
> disp(['norm of backslash residual = '
> num2str(norm(A*x-b),4)]);
> disp(['norm of Cramer''s Rule residual = '
> num2str(norm(A*xc-b),4)]);
> disp(' ');
> return
> end
>
> function x = cramersrule(A,b) % Demo of Cramer's Rule for
> solving A*x = b
> disp('Timing Cramer''s Rule:');
> tic
> [m n] = size(b);
> z = zeros(m,1);
> Ai = A;
> for k=1:m
> Ai(:,k) = b;
> z(k) = det(Ai);
> Ai(:,k) = A(:,k);
> end
> detA = det(A);
> x = z / detA;
> toc
> disp(['Max abs(det(Ai)) = ' num2str(max(abs(z)),4)]);
> disp(['abs(det(A)) = ' num2str(abs(detA),4)]);
> return
> end
>


Thanks one and all ...
I solved my problem ..
All the replies were useful...

Ashwini

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics