The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

MATLAB troubles.

DemerdarDemerdar Registered User regular
edited April 2008 in Help / Advice Forum
Hmm.. more matlab troubles. I swear I barely understand this program. Anyway.. What i'm trying to do is generate polynomials using A Lagrange Interpolation technique. What I want to know is how do I create actual functions is matlab, that give me an output of say y = 5x^3 + 6x^2 + x + 2?

I know there's a way to do it numerically (just put a value for x in there), but I want the general formula. Can MATLAB do this?

y6GGs3o.gif
Demerdar on

Posts

  • PemulisPemulis Registered User regular
    edited April 2008
    Just define the values of x you want to use:

    x=0:5:50

    Where in this example, 0 is the first value, 5 is the step size, and 50 is the final value. This will create x as an array of numbers. Remember that in Matlab, the index of the first element is 1. You can also use specific values with brackets: x=[1,2,3,4, 5:6]. etc. Also note that the default step size is 1, so x=0:1:10 is equivalent to x=0:10.

    Then you just have to define your function, for example:

    y=x.^2

    The . indicates each element in the array should be squared. You also must use this with other operators if element by element math is your goal: y=x.*x is equivalent to the above. You will then get an array for y, equal in length to x.

    Matlab is incredibly powerful so just an intro. Hope this helps.

    Pemulis on
  • grungeboxgrungebox Registered User regular
    edited April 2008
    I think OP wants a way to represent the function algebraically. No, that is not possible in MATLAB (as far as I know). You want something like Maple if you need to do that.

    grungebox on
    Quail is just hipster chicken
  • GdiguyGdiguy San Diego, CARegistered User regular
    edited April 2008
    grungebox wrote: »
    I think OP wants a way to represent the function algebraically. No, that is not possible in MATLAB (as far as I know). You want something like Maple if you need to do that.

    I'll echo that as far as I know as well you can't do this easily... Matlab is really more of a data-analysis program, Maple (i think) or Mathematica are more designed to do mathematics/analysis of actual functions/etc kind of stuff

    is there a particular thing you're trying to do? if you just want a graph you can always make the X array very huge, and if you have a decent computer you should be able to get a smooth curve out of it

    Gdiguy on
  • HorizonXPHorizonXP Registered User regular
    edited April 2008
    Look into the symbolic toolbox. Should have methods for what you wanna do.

    Here's an example of its use:

    f = x^2;
    syms x;
    diff(f)
    int(f)

    should see 2x and x^3. that syms command tells matlab what you've called your variable, so it doesn't try to treat it as a constant.

    HorizonXP on
    HorizonXP.png
  • CycloneRangerCycloneRanger Registered User regular
    edited April 2008
    MATLAB is absolutely capable of doing what you're describing. You can handle simple functions using the symbolic math toolbox (already described), but most of the advanced functions for data analysis will require you to create an actual function (I had to do this the other day using the ode45 command; I couldn't get MATLAB to accept any of my quick-and-dirty symbolic math amalgamations). A function, in MATLAB, is a special type of script file.

    I think this might be what you are looking for. It is specifically about solving differential equations numerically, but contains instructions on how to create functions that MATLAB can read.

    CycloneRanger on
  • DemerdarDemerdar Registered User regular
    edited April 2008
    :^: Aye thanks, the symbolic toolbox was what I was looking for..

    And horizon, you would see 2x and x^3/3 :P

    Case closed.

    Demerdar on
    y6GGs3o.gif
  • HorizonXPHorizonXP Registered User regular
    edited April 2008
    You are correct sir. However, I was typing that with an orange dripping on my fingers, and typing sparingly with one hand to try NOT to get my keyboard sticky, and to provide a correct answer. I failed at both.

    HorizonXP on
    HorizonXP.png
Sign In or Register to comment.