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)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (1)
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
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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!