# Homework Help: Basic MATLAB help

1. Apr 2, 2009

### strokebow

1. The problem statement, all variables and given/known data

This isn't the assignment itself: I want to be able to enter a function into the MATLAB command line.
ie) >> Function = .....

2. Relevant equations

eg)
f(x1, x2, x3) = x1^2 + 2x2^2 + 3x3^2 + 2X1X2- 3X1X3 + 2X2X3

3. The attempt at a solution

Can't find anything on the internet. Tutorials are either too basic or too advanced.

Thanks

2. Apr 2, 2009

### KLoux

I'm not sure I understand what you want to do...

Can you write an m-file that takes arguments x1, x2, and x3 so you can call:

>> your_func(1, 2, 3)

If this doesn't help, what exactly are you trying to do?

-Kerry

3. Apr 2, 2009

### strokebow

Hi,

The function I gave as an example above - is there a way I can create a variable on the command line which represents that function???

I read on the net about creating polynomial functions on the command line, such as:

Is there a similar approach I can take for an n-dimensional quadratic?

4. Apr 2, 2009

### KLoux

I don't know of any built-in MATLAB function that does this, but you can certainly write your own function that does this...

Are you familiar with functions?

I don't think I follow your polynomial creation either, though. Entering a vector x = [1 2 3] gives you a vector x = [1 2 3]... not a polynomial. Are you missing another function call? How do you evaluate your polynomial at s = 6, for example, or is this not what you want to do with it?

-Kerry

5. Apr 2, 2009

### strokebow

That quote was from: http://www.engin.umich.edu/group/ctm/basic/basic.html

All I want to do is to be able to use a function, such as:
f(x1, x2, x3) = x1^2 + 2x2^2 + 3x3^2 + 2X1X2- 3X1X3 + 2X2X3

in MATLAB.

Would I have to write an .m file to do this?
I wouldn't know where to start???

6. Apr 2, 2009

### KLoux

Ahha, I see what you're talking about:

In MATLAB, commands like roots() and polyval() take a vector and interpret it as a polynomial. There is no built-in way for MATLAB to interpret a vector as an n-th order quadratic. For each function that you want to use, you would have to create your own function (m-file) to do this. What functions are you looking for? Do you need to evaluate your function at a point, find roots, anything else?

At MATLAB's command prompt, you can type 'help function' to read a little bit about it, but functions are so general and so powerful, that the only real way to learn about them is to try and use them. You can use them for very simple tasks, or very complex tasks. Your task is probably somewhere in the middle, maybe a little closer to the simple side, depending on how much experience you have with other programming languages.

Here is my 10 second intro to functions:

Create an m-file and save it in your working directory (do you understand the working directories?). The name of the m-file should be "your_function_name.m". Here, we'll make a function that adds an arbitrary number of inputs and call it sum_of_n_numbers.m.

Code (Text):

function sum = sum_of_n_numbers(numbers)
%  The first line of the function must take this format:
%  function output = name_of_function(input1, input2, ...)
%  If you type "help sum_of_n_numbers" at the MATLAB prompt, the text that you
%  place here (commetns immediately after the function declaration) is what will
%  be displayed.
%

%  If you want to (not a bad idea), you can add a check here to make sure
%  the input is a vector, and not a matrix...

%  Get the number of numbers
n = max(size(numbers));

sum = 0;

%  Add the numbers one by one
for i = 1:1:n%  Start at 1, step by 1, go to n
sum = sum + numbers(i);
end

%  All done!
return;

MATLAB has a decent debugger, too, which you'll probably use a lot. Another trick I use a lot is to remove the ; at the end of a line where you want to print the value of the assignment.

Hope this helps,

Kerry

7. Apr 2, 2009

### strokebow

Thanks Kerry!