PDA

View Full Version : Matlab Error: Undefine Variable


50Cent
Nov3-09, 08:02 AM
Hi,

I am trying to model a simple 1DOF suspension system [mass(ms) 300kg, stiffness(k) 15000N/m, damping coefficient(c) 2500Ns/m. Its subjected to different inputs and then needs to simulate the dynamic response of the system to a road surface that has the characteristics of a flat road, a sine wave surface and then a step.

I completed the model. However, i have now opened the file again and i keep getting the error message:

Error evaluating parameter 'Numerator' in 'HC1/Transfer Fcn': Undefined function or variable 'c'

I have defined "c" as 2500 in the "m" file and the matlab working directory is correct as far as i know. I have attached a screen shot of the error. Does any1 know how to solve this problem?

Mark44
Nov3-09, 11:23 AM
I can't tell much from your screen shot. Can you show us your code for 'HC1/Transfer Fcn'? One possible reason for your problem is that you might be using c as if it were a global variable, but it's not. If c is defined in one function and you attempt to use it in another function, I think you will get the error you showed.

If you want to be able to use c in all your functions, make it a global variable. Here is some info about global variables from the online Getting Started documentation for MatLab (http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf)

Global Variables
If you want more than one function to share a single copy of a variable, simply
declare the variable as global in all the functions. Do the same thing at
the command line if you want the base workspace to access the variable.
The global declaration must occur before the variable is actually used in a
function. Although it is not required, using capital letters for the names of
global variables helps distinguish them from other variables. For example,
create an M-file called falling.m:
function h = falling(t)
global GRAVITY
h = 1/2*GRAVITY*t.^2;
Then interactively enter the statements
global GRAVITY
GRAVITY = 32;
y = falling((0:.1:5)');

The two global statements make the value assigned to GRAVITY at the
command prompt available inside the function. You can then modify GRAVITY
interactively and obtain new solutions without editing any files.

50Cent
Nov5-09, 07:55 AM
Hi,

Sorry for the late reply, i have been caught up doing other coursework. I have provided a screen shot of the code for "'HC1/Transfer Fcn"

I will look into changing the global variable. Thanks