×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

how to work with variables in matlab

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

RE: how to work with variables in matlab

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

RE: how to work with variables in matlab

(OP)
Is it not possible to first define the matrix and the variables afterwards?

Greatings,
Hans

RE: how to work with variables in matlab

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

RE: how to work with variables in matlab

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

RE: how to work with variables in matlab

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources