Basic MATLAB Help: Entering a Quadratic Function

  • Thread starter Thread starter strokebow
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Homework Help Overview

The original poster seeks assistance with entering a quadratic function into MATLAB, specifically how to represent an n-dimensional quadratic function in the command line. They provide an example of such a function and express difficulty in finding relevant resources online.

Discussion Character

  • Exploratory, Conceptual clarification, Problem interpretation

Approaches and Questions Raised

  • Some participants suggest creating an m-file to define the function, while others question the original poster's understanding of polynomial representation in MATLAB. There are inquiries about the specific goals for using the function, such as evaluation or finding roots.

Discussion Status

The discussion is ongoing, with participants providing guidance on creating functions in MATLAB and exploring the limitations of built-in functions for handling n-dimensional quadratics. There is a lack of consensus on the best approach, as multiple interpretations of the original poster's needs are being explored.

Contextual Notes

Participants note that there may be confusion regarding the representation of polynomials and functions in MATLAB, as well as the necessity of understanding working directories when creating m-files.

strokebow
Messages
122
Reaction score
0

Homework Statement



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


Homework Equations



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

The Attempt at a Solution



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


Your help is much appreciated

Thanks
 
Physics news on Phys.org
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)

and get your result?

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

-Kerry
 
Hi,

Thanks for your reply!

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:

To create a polynomial in Matlab, simply enter each coefficient of the polynomial into the vector in descending order. For instance, let's say you have the following polynomial:

S^4 + 3S^3 - 15S^2 - 2S + 9

To enter this into Matlab, just enter it as a vector in the following manner

x = [1 3 -15 -2 9] x = 1 3 -15 -2 9
Is there a similar approach I can take for an n-dimensional quadratic?
 
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
 
KLoux said:
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

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?
 
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.

Copy this into your m-file:
Code:
function sum = sum_of_n_numbers(numbers)
%  The first line of the function must take this format:
%  function output = name_of_function(input1, input2, ...)
%  Add other information about your function here (the % is the comment character)
%  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));

%  Initialize your output
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
 
Thanks Kerry!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
2K