Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to work with variables in matlab 1

Status
Not open for further replies.

hniessen

Industrial
Apr 24, 2002
4
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
 
Replies continue below

Recommended for you

Hans

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
 
Is it not possible to first define the matrix and the variables afterwards?

Greatings,
Hans
 
Hans

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
 
I am trying to solve an equation arithmetically using the solve() function. The problem is that I want the coefficients to be variables, so that I can call the solve() function inside a for loop and still be able to solve the equation for different values of the coefficients.

Matlab responds with an error, because it cannot recognize symbolic names as coefficients...

Thanx a lot!
jpg
 
Hello hniessen and jpg,

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor