Cramer's Rule Homework Help

Hello,
I am having a lot of trouble with this homework because I am new to matlab. Please help!
Question:
Create a MATLAB script that will read in system of linear equations (SOLE) stored in an excel file (the format will be described in more detail below) and solve for all variables using Cramer's rule. You may assume that you will always be given the same number of equations as there are number of variables, i.e. if there are three variables (x, y, z) then there will be three equations. However, your code should be able to work for an arbitrary size system of linear equations. That means you cannot assume and hardcode the size of the SOLE in your code. Use the size function in Matlab.
I am not looking for the answer, but help and hints on how to go about doing this. This is what I have based on some of my previous knowledge and what I have seen online.
data = xlsread('sole_ex1.xlsx');
x = cramer(A,b);
if det(A) == 0; error('Matrix is invalid'); end
[z,z] = size(A); for g = 1:z;
B = A;
B (:,g) = b;
x(g) = det(B)/det(A);
end
x = x';
Thank You, Ryan Albawab

1 个评论

how to find the value of x and y by using Cramer’s rule from these equations:
𝑎𝑥+𝑏𝑦=𝑐 and 𝑑𝑥+𝑒𝑦=𝑓 ???

请先登录,再进行评论。

 采纳的回答

What you wrote in there should work. I am guessing you are actually having trouble with syntax.
data = xlsread('sole_ex1.xlsx');
Here you have to make sure the data you read is in a format you want. I can't help you there without the excel file but you need to explicitly assign A and b. Right now you are just calling the entire batch from excel simply data.This pretty much the only place your code is broken.
x = cramer(A,b);
Get rid of this. I am assuming you tried to define a function here, not necessary according to your homework prompt.
if det(A) == 0; error('Matrix is invalid'); end
z = size(A,1); % Simply better practice.
x = zeros(z,1);
for g = 1:z;
B = A;
B (:,g) = b;
x(g) = det(B)/det(A);
end

7 个评论

Ahmet,
I have attached the excel file that was assigned with this question. I am also taking another approach. Am I going in the right direction?
file = 'sole_ex1.xlsx';
data = xlsread(file);
[A,b] = size(data);
x = cramer(A,b);
So your data is a n by n+1 augmented matrix. You have to take the first n by n section and make it A, the last column and make it b. Since this is so fundamental MATLAB indexing, I will give the answer to you straight out. My suggestion to you is do some beginner exercises to learn the syntax.
data = xlsread('sole_ex1.xlsx');
A=data(1:size(data,1),1:size(data,1));
b=data(:,size(data,2));
if det(A) == 0; error('Matrix is invalid'); end
z = size(A,1); % Simply better practice.
x = zeros(z,1);
for g = 1:z;
B = A;
B (:,g) = b;
x(g) = det(B)/det(A);
end
Ahmet,
Thank you for your help, and yes I really do need to work on some beginner exercises to learn matlab. I have a question about the code you just wrote. z = size(A,1) x = zeros(z,1) What does that part do? I'm especially confused to why you used the zeros function.
Thank you! Ryan
Its called memory allocation. For your problem it is inconsequential, but as a good coding practice, when you are assigning x(1), x(2) etc in a for loop, it is better if you create a vector x with a predefined length, and replace the values in it, rather than adding values at the end of the vector every time.
size(A) is [4,4], size(A,1) simply grabs the first size, which is 4. You only care about one of the sizes since the matrix has to be square.
That is very helpful.
I looked up online what a zeros function does, it creates a matrix filled with zeros based on the dimensions you input. I am confused as to how this was used to solve the question. I am starting to think that you created a zeros function and then used the data from the file and input it into the zeros function. Is that true?
thank you very much, Ryan
No, I created a vector 'x' filled with 0s that is the same length as your data. Then I replace each 0 with the actual result later. The code will work just fine if you erase x = zeros(z,1);. Again, a 30 min tutorial on MATLAB will answer every question you can possibly have about this script. There are no tricks involved.
Sorry for my incompetence, and thank you for your time.
If you could post a link of the tutorial you are speaking of, I would really appreciate it, thank you. Ryan

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by