Basic MATLAB Help: Entering a Quadratic Function

  • Thread starter strokebow
  • Start date
  • Tags
    Matlab
In summary, Kerry is trying to figure out how to enter a function into the MATLAB command line, and is looking for help. He is not sure if he needs to write an .m file or if MATLAB can interpret a vector as an n-th order quadratic. He also mentions that MATLAB has a decent debugger.
  • #1
strokebow
123
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
  • #2
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
 
  • #3
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?
 
  • #4
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
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?
 
  • #6
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
 
  • #7
Thanks Kerry!
 

1. What is a quadratic function?

A quadratic function is a mathematical function of the form f(x) = ax^2 + bx + c, where a, b, and c are constants and x is the independent variable. It is a type of polynomial function and is commonly used in algebra and calculus.

2. How do I enter a quadratic function in MATLAB?

To enter a quadratic function in MATLAB, you can use the "poly2sym" function to create a symbolic representation of the function. For example, if your quadratic function is f(x) = 3x^2 + 5x + 2, you can enter it as "f = poly2sym([3 5 2], x)". Alternatively, you can also use the "syms" command to define symbolic variables and then use them to create the function.

3. What is the syntax for plotting a quadratic function in MATLAB?

The syntax for plotting a quadratic function in MATLAB is "ezplot(f, [xmin, xmax])", where f is the function and [xmin, xmax] is the range of x values to be plotted. You can also use the "plot" function if you have already defined your function as a symbolic expression.

4. How do I find the roots of a quadratic function in MATLAB?

To find the roots of a quadratic function in MATLAB, you can use the "roots" function. For example, if your quadratic function is f(x) = x^2 - 3x + 2, you can enter "roots([1 -3 2])" to find the two roots, which in this case are x = 1 and x = 2.

5. Can I solve a system of quadratic equations in MATLAB?

Yes, you can use the "solve" function to solve a system of quadratic equations in MATLAB. However, the equations must be in the form of symbolic expressions. For example, if you have the equations f(x) = 2x^2 + 3x + 1 and g(x) = x^2 + 2x + 1, you can enter "solve(f == g, x)" to find the value of x that satisfies both equations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
983
  • Introductory Physics Homework Help
Replies
19
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
3K
Back
Top