how to work with variables in matlab
how to work with variables in matlab
(OP)
In Matlab I have defined follow matrix
R=[0, -k2-b2, k2+b2+m2, -1; -k1-b1, k1+k2+b1+b2+m1, -k2-b2, 1]
How can I ascribe values to the variables k1 k2 b1 b2 m1 and m2 and update the matrix R?
greatings,
Hans
R=[0, -k2-b2, k2+b2+m2, -1; -k1-b1, k1+k2+b1+b2+m1, -k2-b2, 1]
How can I ascribe values to the variables k1 k2 b1 b2 m1 and m2 and update the matrix R?
greatings,
Hans





RE: how to work with variables in matlab
Just assign the variables with the equal sign before assigning the matrix. Put either a comma or a semicolon in between (a comma will echo, a semicolon has no output)
ie:
k1=5, k2=2, b1=3, b2=-9, m1=45.3, m2=12.1
then enter the matrix
R=[0, -k2-b2, k2+b2+m2, -1; -k1-b1, k1+k2+b1+b2+m1, -k2-b2, 1]
the output is
R =
0 7.0000 5.1000 -1.0000
-8.0000 46.3000 7.0000 1.0000
Cheers
J. Vorwald
RE: how to work with variables in matlab
Greatings,
Hans
RE: how to work with variables in matlab
In matlab, the variables have to be defined before being used, otherwise you get the error "Undefined function or variable"
However, you could define a function, with the variables defined as parameters. That way, you can change the variables and have the matrix recalculated.
The only language that I know of that allow you to use variables before defining them is Maple. In Maple, the variables are treated symbolically, and don't ever have to be assigned a numeric value.
Here's an sample program illustrating the use of functions and recalculating the matrix.
function test
k1=5; k2=2; b1=3; b2=-9; m1=45.3; m2=12.1;
r1 = myMatrix(k1, k2, b1, b2, m1, m2)
m2 = 11;
r2 = myMatrix(k1, k2, b1, b2, m1, m2)
function R = myMatrix(k1, k2, b1, b2, m1, m2)
R=[ 0, -k2-b2, k2+b2+m2, -1
-k1-b1, k1+k2+b1+b2+m1, -k2-b2, 1];
The output is
r1 =
0 7.0000 5.1000 -1.0000
-8.0000 46.3000 7.0000 1.0000
r2 =
0 7.0000 4.0000 -1.0000
-8.0000 46.3000 7.0000 1.0000
Cheers
J. Vorwald
RE: how to work with variables in matlab
Matlab responds with an error, because it cannot recognize symbolic names as coefficients...
Thanx a lot!
jpg
RE: how to work with variables in matlab
Matlab can do what you are asking, but you will need to get the Symbolic Math Toolbox in order to be able to do it.
Once you have it, then your variables are defined as
"sym k1, k2, b2, etc". You use "solve" to get an algebraic solution. You then can plug in actual values and get a numeric solution to your matrix.
I hope this helps.