How do I create a vector or matrix of variables with undefined variables using matlab or is that even possible?

5 Ansichten (letzte 30 Tage)
I am creating a script to solve for the current at each resistor in a circuit. I have created the A[6x6] matrix, a x[6x1] matrix which i am trying to solve for, and B[6x1] matrix. The x matrix consist of 6 variables that is being solved for. When I run the script I receive and error because the x matrix consist of undefined variables.
The following code is what i have at this time:
fprintf('Circuit Analysis Problem Part 1')
% resistance
R_1 = 5 R_2 = 10 R_3 = 15 R_4 = 10 R_5 = 5 R_6 = 20
% Voltage
v_s = 200
% A Matrix
fprintf('Matrix A coefficients of i')
A = [ -1 1 0 0 0 0; 1 0 -1 0 0 0; 0 0 1 1 0 -1; 0 -1 0 -1 1 0; -R_1 -R_2 -R_3 R_4 0 0; 0 0 0 -R_4 -R_5 -R_6]
% X Vector
fprintf('Vector representing all current values')
x = [ i_1 i_2 i_3 i_4 i_5 i_6]
% B Matrix
fprintf('Vector representing the voltage')
B = [ 0 0 0 0 0 v_s]
% Equation
fprintf('The following vector is a representation of all i values')
x' == A\B'
Thanks for your help!

Antworten (1)

Stephen23
Stephen23 am 30 Jun. 2018
Bearbeitet: Stephen23 am 30 Jun. 2018
Get rid of that x vector (which is not used for anything, and causes some of your problems), ensure that your matrices have the correct orientation, and write the equation solver code correctly:
>> R_1 = 5;
>> R_2 = 10;
>> R_3 = 15;
>> R_4 = 10;
>> R_5 = 5;
>> R_6 = 20;
>> v_s = 200;
>> A = [-1,1,0,0,0,0;1,0,-1,0,0,0;0,0,1,1,0,-1;0,-1,0,-1,1,0;-R_1,-R_2,-R_3,R_4,0,0;0,0,0,-R_4,-R_5,-R_6];
>> B = [0,0,0,0,0,v_s];
>> x = A\B.'
x =
-1.5385
-1.5385
-1.5385
-4.6154
-6.1538
-6.1538
And to answer your original question: "How do I create a vector or matrix of variables with undefined variables using matlab or is that even possible?"
Normal MATLAB does numeric computing, where every variable is fundamentally numeric and has to have a value: it is not possible for a computer to define a uint8 number that has no value. The floating point classes include NaN and infinity values, but these are not "undefined variables" in the sense that you mean.
However there are several additions to MATLAB that let you perform symbolic/algebraic operations, such as the Symbolic Toolbox, MuPAD notepbooks, or Maplesoft's toolbox, etc. These toolboxes allow variables to be defined as algebraic values, i.e. as a dependent or independent variable, etc, which can then be solved for or manipulated algebraically. Note that symbolic maths is generally computationally much less efficient than the "equivalent" numeric maths.
  4 Kommentare
Andrew Hartman
Andrew Hartman am 2 Jul. 2018

The following is the code I have so far.

fprintf('Section 7.2 Problems 11 - 20')

A = [4 -2 3; -2 1 6; 1 2 2]

B = [1 -3 0; -3 1 0; 0 0 -2]

C = [0 1; 3 2; -2 0]

a_1 = [1 -2 0]

b_2 = [3; 1; -1]

fprintf('Problem 11 parts a - d ')

a11 = A * B

b11 = A * B'

c11 = B * A

d11 = (B') * A

fprintf('Problem 12 parts a - d')

a12 = A * A'

b12 = A^2

c12 = B * B'

d12 = B^2

fprintf('Problem 13 parts a - d')

if (C * C') == True

a13 = C * C' end

b13 = B * C

c13 = C * B

d13 = (C') * B

fprintf('Problem 14 parts a - d')

a14 = 3 * A - 2 * B

b14 = (3 * A - 2 * B)'

c14 = 3 * A' - 2 * B'

d14 = (3 * A - 2 * B)' * a_1'

fprintf('Problem 15 parts a - b')

a15 = A * a_1

b15 = A * a_1'

c15 = (A * b_2)'

d15 = b_2' * A'

fprintf('Problem 16 parts a - d')

a16 = B * C

b16 = B * C'

c16 = B * b_2

d16 = b_2' * B

fprintf('Problem 17 parts a - d')

a17 = A * B * C

b17 = A * B * a_1

c17 = A * B * b_2

d17 = C * a_1'

fprintf('Problem 18 parts a - d')

a18 = a_1 * b_2

b18 = b_2 * a_1

c18 = a_1 * A

d18 = B * b_2

fprintf('Problem 19 parts a - d')

a19 = 1.5 * a_1 + 3.0 * b_2

b19 = 1.5 * a_1' + 3.0 * b_2

c19 = (A - B) * b_2

d19 = A * b_2 - B * b_2

fprintf('Problem 20 parts a - d')

a20 = b_2' * A * b_2

b20 = a_1 * B * a_1'

c20 = a_1 * C * C'

d20 = C' * b_2 * a_1

As I stated before some of the problems have no answer, I want to have a loop that will process each problem and display the answer if one exists and display N/a if one does not.

Image Analyst
Image Analyst am 3 Jul. 2018
Don't double space. Read this link to learn how to do it right so your post won't get marked as spam.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by