Cramer's Rule Homework Help

38 次查看(过去 30 天)
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 个评论
Rakib Seemanto
Rakib Seemanto 2020-11-1
how to find the value of x and y by using Cramer’s rule from these equations:
𝑎𝑥+𝑏𝑦=𝑐 and 𝑑𝑥+𝑒𝑦=𝑓 ???

请先登录,再进行评论。

采纳的回答

Ahmet Cecen
Ahmet Cecen 2015-4-22
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 Cecen
Ahmet Cecen 2015-4-22
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.
Ryan Albawab
Ryan Albawab 2015-4-22
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 个)

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by