MATLAB Answers

# What next in the code

1 view (last 30 days)
MINATI on 15 Jan 2019
Edited: Stephen Cobeldick on 15 Jan 2019
While running the code
%NATURAL FREQUENCIES AND MODE SHAPES FOR A 4 STOREY BUILDING
k=[71284.72 -35642.36 0 0;-35642.36 71284.72 -35642.36 0;0 -35642.36 71284.72 -35642.36;0 0 -35642.36 35642.36];
m=[632.25 0 0 0;0 632.25 0 0;0 0 632.25 0;0 0 0 363.75];
W=Naturalfreq(k,m)%Function 1
X=zeros(4,4);
for i=1:4
Y=Modeshape1(W(5-i),k,m); %Function 2
X(:,i)=Y;
plot(X)
end
Function 1
[W] = Naturalfreq(k,m)
syms omega
a = k-(omega*m);
b = det(a);
c = sym2poly(b);
d = roots(c);
W = sqrt(d);
Function 2
[X] = Modeshape1(W,k,m)
omega = W^2;
a = k - (omega*m);
x1 = 1;
x2 = -a(1,1)/a(1,2);
x3 = -(a(2,1)+a(2,2)*x2)/a(2,3);
x4 = -(a(3,2)*x2+a(3,3)*x3)/a(3,4);
X = [x1;x2;x3;x4]
The following error occurs
Undefined function or variable 'Naturalfreq'.
Error in
W=Naturalfreq(k,m)%Function 1

#### 0 Comments

Sign in to comment.

### Accepted Answer

KSSV on 15 Jan 2019
The error clearly says that you don;t have the function/ code named Naturalfreq.m . You have to donwload and save it and use.

#### 4 Comments

Show 1 older comment
Stephen Cobeldick on 15 Jan 2019
"The error clearly says that you don;t have the function/ code named Naturalfreq.m"
Remember that functions do not have to be Mfiles (e.g. anonymous function, local function, etc.), so that .m is misleading: the error message itself just states the function/variable name.
"You have to donwload and save it and use."
The code for both functions is already shown, there in the question. Where should this code be downloaded from? Why bother, when it is already there? How is this advice relvant?
madhan ravi on 15 Jan 2019
"Remember that functions do not have to be Mfiles (e.g. anonymous function, local function, etc.), "
"so that .m is misleading:"
@Stephen Cobeldick: Not completely true , versions prior to 2016b - functions have to be saved in a separate file. How is this advice relevant to the fact ? Did you even verify before stating that ?
Stephen Cobeldick on 15 Jan 2019
"Not completely true , versions prior to 2016b - functions have to be saved in a separate file."
Anonymous functions have never needed to be saved in a separate file, and in my previous comment I specifically listed anonymous functions: that error message could easily refer to a missing anonymous function (which is why the .m is misleading). Local functions are, by definition, saved in the same file as a script or a main function, and do not have .m in their names.
Your critique refers only to main functions (i.e. saved as the first function in an Mfile), to which my previous comment does not refer to at all, and in fact I specifically excluded these by writing "Remember that functions do not have to be Mfiles ...". It is also worth nothing that the MATLAB documentation clearly distinguishes between the function name and the filename (which does indeed have .m): "The name of the file should match the name of the first function in the file" (although some beginners apparently do confuse the two).
"How is this advice relevant to the fact ?"
Adding the .m is misleading as the error message could refer to:
• a variable (variables do not permit .m in their names).
• an anonymous function or a local function (neither of which allow .m in their names), exactly as I wrote in my previous comment.
• a main function (something that I did not mention at all in my previous comment, but which also do not have .m in their function names, only in the filename).
Note that functions saved inside a script do not have .m in their names either, as they are considered to be local functions (thank you for another example!):
"Did you even verify before stating that ? "
Yes, I verified that variable names cannot contain the period character:
"A valid variable name starts with a letter, followed by letters, digits, or underscores."
And also that function names cannot contain the period character:
"Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscores."
Note that this applies to main functions as well, which I would consider quite "relevant to the fact".

Sign in to comment.

### More Answers (1)

Stephen Cobeldick on 15 Jan 2019
Edited: Stephen Cobeldick on 15 Jan 2019
Your function definitions are not correct. Instead of this invented syntax:
Function 1
[W] = Naturalfreq(k,m)
... your code
you should follow the syntax shown in the function documentation:
function W = Naturalfreq(k,m)
... your code
end % you need this too!
I highly recommend that you read about how to include functions into scripts:

#### 0 Comments

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!