Defining a Function in MATLAB M-file: Troubleshooting Error

In summary, the code is using the bisection method to find the root of a function using a for loop. However, there is an error with the IF statement and the code is not running properly.
  • #1
JohanL
158
0
Im trying to define a function in a MATLAB m-file.

function y = f(x)
global T;

f = 1/(x^5*(exp(h*c/(k*x*T))-1));

but it doesn't work...matlab says

? Input argument 'x' is undefined.

Error in ==> D:\Program\matlab\work\f.m
On line 6 ==> y = 1/(x^5*(exp(h*c/(k*x*T))-1));

Error in ==> D:\Program\matlab\work\a.m
On line 14 ==> lmax(i)=fminbnd(-f,0,1*10^(-6));
 
Physics news on Phys.org
  • #2
By the looks of it, you need to define more than just x; k, c and T for example. Also did you mean to type "y =..." and not "f = ..."? 'y' is your return variable.

Edit: try sending in the values in the command window, too. In this case: var = f(3), assuming that your function takes in an interger.
 
Last edited:
  • #3
JohanL said:
Im trying to define a function in a MATLAB m-file.

function y = f(x)
global T;

f = 1/(x^5*(exp(h*c/(k*x*T))-1));

but it doesn't work...matlab says

? Input argument 'x' is undefined.

Error in ==> D:\Program\matlab\work\f.m
On line 6 ==> y = 1/(x^5*(exp(h*c/(k*x*T))-1));

Error in ==> D:\Program\matlab\work\a.m
On line 14 ==> lmax(i)=fminbnd(-f,0,1*10^(-6));


Yes, you are doing some things wrong.
if you define a function in a m file like this:
function y = f(x)
then x is the input, y is the output and f is the name of the function (you also have to save the m-file as f.m), so what does that mean, let's say your m-file is like this:

function y = f(x)

y = x^2 + 5x;

then you can use this function by typing in the command window:
f(3)
if you do that you will get the reply:
ans = 24

What has happened? Well Matlab saw that you called a function that is named f it sees that this function exists in the file f.m and it sees that what the function should do is: square the input (in this case 3) add 5 times the input to this and assign the result to y
(that is what you defined by the line: y = x^2 + 5)
Matlab also sees that you want to output y
(that is what you have defined by the line: function y = f(x) )
so Matlab will calculate y (in this case 24) and output that
 
  • #4
Thank you for your answers. But it I can't figure it out.

It works fine when i type f(3) in the command window.

Do i have to declare that x is a vector between some values.
Ive tried
x = linspace(a,b) where a and b are the values.
But then x^5 in the function don't work and x(i)^5 don't work either.

(I have defined the constants but to save space and time I didnt write them here. The m-file is saved correctly.)

The m-files are

f.m

function y = f(x)
global T;
c=2.9979*10^8;
k=1.3805*10^(-23);
h=6.6256*10^(-34);

y = 2*pi*h*c^2/(x^5*(exp(h*c/(k*x*T))-1));

_______________________________________

a.m

clear
clf

global T;

c=2.9979*10^8;
k=1.3805*10^(-23);
h=6.6256*10^(-34);

for i=1:3

T(i)=1000*i;
lmax(i)=fminbnd(-f,1*10^(-8),1*10^(-6));
c(i)=lmax(i)*T(i)
end
 
  • #5
I'm also new to MATLAB but when you want to raise the power of each element of a vector you need to put a dot in, e.g:

x=[1 2 3]

x.^3

[1 8 27]
 
  • #6
matlab still complains about

? Input argument 'x' is undefined.
 
  • #7
You should define x in the instruction program (a.m I think it is its name).

Matlab doesn't know what on Earth is x if he hasn't got information about.

Try to define x as a vector of your coordinates and use the vectorial operator .^ for powering it.
 
  • #8
JohanL said:
matlab still complains about

? Input argument 'x' is undefined.

yes, you get that error message because of this line in a.m:
lmax(i)=fminbnd(-f,1*10^(-8),1*10^(-6));
you call the function f , but you do not specify what x is, you should do something like this:
lmax(i)=fminbnd(-f(3),1*10^(-8),1*10^(-6));
of course it does not have to be 3.
 
  • #9
Thank you ...now it works.
I also found that before f in
lmax(i)=fminbnd(f,1*10^(-8),1*10^(-6));
you need to have an @ like this
lmax(i)=fminbnd(@f,1*10^(-8),1*10^(-6));
 
  • #10
Just curious, why is it called Matlab and not Mathlab?
 
  • #11
because it stands for Matrix Laboratory
 
  • #12
%PID Controller Simulation Step Function m-File

Kp=10;
Kd=8;
num=[0 Kd Kp];
den=[2 2+Kd 2+Kp];

t=0:0.01:6;
step(num, den,t)
u=num/den;

%MDS Simulation in Z Direction
%Function m-File:
function pdot=mds(t,p)
%Spring Example Problem
m=2; g=-9.81; Kp=10; Kd=8;
%global u tautheta tauphi taupsi
%Parameters-damping coefficient and natural frequency
%forcing function

u=1;
tautheta=1;
tauphi=1;
taupsi=1
pdot=zeros(size(p));
pdot(1)=p(4);
pdot(2)=p(5);
pdot(3)=p(6);
pdot(4)=-u/m*sin(p(7));
pdot(5)=u/m*cos(p(7))*sin(p(8))
pdot(6)=u/m*cos(p(7))*cos(p(8))-g
pdot(7)=p(10);
pdot(8)=p(11);
pdot(9)=p(12);
pdot(10)=tautheta;
pdot(11)=tauphi;
pdot(12)=taupsi;

%ODE Solver m- File:
clear; close all; clc;

dt=0.01; 64

tspan=[0:dt:10];
global u;
% t=1:.1:10;

p0=[0;0;0;0;0;5;0;0;0;0;0;0];

[t,p]=ode45('mds', tspan, p0);

xdot=p(:,2);
x=p(:,1);

figure
subplot(2,1,1)
plot(t,x);
xlabel('time(s)'); ylabel('displ(m)');
subplot(2,1,2)
plot(t,xdot);

I executed in matlab, following error msg shown when i run mds.m file. please help me.

? Input argument "p" is undefined.

Error in ==> mds at 14
pdot=zeros(size(p));
 
  • #13
hey i have an error with line where the IF statement is and i idk what is it
i have another function file called f and i tested its working please help :)


a=0;
b=pi/2;
for i=1:10
xr= (a+b)/2;
if f(b)*f(xr)<0
a=xr;
else
b=xr;
end
end
xr
 

1. What is a function in MATLAB M-file?

A function in MATLAB M-file is a block of code that performs a specific task and can be called multiple times in a program. It helps in organizing and simplifying complex code by breaking it into smaller, modular pieces.

2. How do I define a function in MATLAB M-file?

To define a function in MATLAB M-file, use the keyword "function" followed by the function name and input arguments in parentheses. Then, use the keyword "end" to mark the end of the function. Here's an example:
function output = functionName(input1, input2)
% function body
end
Note: The function name and file name should be the same.

3. What does the error "Undefined function or variable" mean?

This error means that MATLAB cannot find the function or variable that you are trying to use. This can happen if you have not defined the function or variable, or if the function or variable is not in the current working directory. Make sure to define the function or add the directory where the function is located to your MATLAB path.

4. How do I troubleshoot errors in my MATLAB function?

To troubleshoot errors in your MATLAB function, start by checking the syntax and making sure all the necessary inputs are provided. You can also use the MATLAB debugger to step through your code and identify where the error is occurring. Additionally, you can use "disp" statements to display the values of variables at different points in your code to help identify the issue.

5. Can I call a function from another function in the same M-file?

Yes, you can call a function from another function in the same M-file. This is known as a nested function. To do this, define the nested function within the main function using the keyword "function" and call it like any other function. Note: The nested function can only be called from within the main function and cannot be accessed outside of it.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
993
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top