Understanding Basic MATLAB Functions: How to Use and Define Them

In summary, the conversation is about using MATLAB to solve a differential equation. The person is having trouble understanding why their code is not working and is seeking help. They are directed to the online documentation for MATLAB's ODE solvers and are advised to bookmark the MATLAB documentation webpage for more comprehensive information. The conversation ends with the person expressing gratitude for the help.
  • #1
rem88
5
0
Hi, sorry to ask this, i know its really simple. I tried to look online for help but i couldn't understand why i was going wrong.

I have a function u = [0.25*s]/[0.005 + s]

I put this in a .m file.

I am going to use this by puting an initial value (so) into find u. Then u is used in a differential equation to find a new s.

but when i for example write, s=2 iget this message


>> s=2

s =

2

>> u
? Input argument "s" is undefined.

Error in ==> u at 2
u = [0.25*s]/[0.005 + s]
>>



this is my m file

function u = f(s)
u = [0.25*s]/[0.005 + s]

i haven't really used mat lab before so any help would be great.

Thanks, rem
 
Physics news on Phys.org
  • #2
Sorry, just figrued it out, i have to put in u(2)

But does anyone know how i know use this to put into a differentail equation (useing ODE45) to generate a new value for s. (which will b e used again in the differential)
 
  • #3
You may want to take a look at the online documentation for how to structure arguments going into one of MATLAB's ODE solvers:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/ode23.html

Additionally, I've posted this before in this forum, but MATLAB scripts and functions (those are the m-files you posted previously):
http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f4-2525.html

If you're just starting out with MATLAB, I highly recommend book marking the MATLAB documentation webpage... It's more comprehensive and detailed than the built-in MATLAB documentation (which is decent for on-the-fly reference). If you wanted details about, say, the 'eye' function (identity matrix generator) you would just type in

>> help eye
 
Last edited by a moderator:
  • #4
Thanks, the links you hav posted look very helpul.

Than you :smile:
 

1. What is the basic syntax for creating a function in Matlab?

The basic syntax for creating a function in Matlab is as follows:
function [output] = functionName(input1, input2, ...)
The function keyword is followed by the name of the function and a pair of parentheses that contain the input parameters. The output of the function is optional and is specified in square brackets. The code for the function is then written between the function and end keywords.

2. How do I call a function that I have created in Matlab?

To call a function in Matlab, you simply type the name of the function followed by the input parameters in parentheses. For example, if you have a function called multiply that takes in two numbers, you would call it like this:
multiply(4, 5)
This would return the product of 4 and 5, which is 20.

3. Can I have multiple outputs from a single function in Matlab?

Yes, you can have multiple outputs from a single function in Matlab. To do this, you need to specify the outputs in square brackets after the function name, separated by commas. For example, if your function calculates the area and perimeter of a rectangle, you could specify the outputs as follows:
function [area, perimeter] = rectangleCalc(length, width)
You can then call the function and assign the outputs to variables, like this:
[a, p] = rectangleCalc(4, 6)
The variables a and p would then contain the area and perimeter, respectively.

4. How do I pass variables by reference in a Matlab function?

In Matlab, variables are always passed by value, meaning that a copy of the variable is passed to the function. However, you can achieve a similar effect to passing by reference by using the global keyword. This allows you to access and modify global variables from within a function. For example, if you want to modify a global variable called count in your function, you would first declare it as global in your function like this:
function myFunction()
global count

You can then access and modify the count variable within the function.

5. How can I debug my Matlab function if it's not working correctly?

There are a few ways to debug a Matlab function if it's not working correctly. One way is to insert disp statements within your function to display the values of variables at different points in the code. Another way is to use the dbstop command to set breakpoints in your code, which allows you to step through the code and see the values of variables as the program runs. Additionally, you can use the fprintf function to print out information to the Command Window as your function runs.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
996
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
806
Back
Top