Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB Code help!

  1. Jan 22, 2010 #1

    First of all, I am not trying to "spam" subforums. I found out that my thread shouldn't be posted under homework. Anyways, here it is.

    Let say there's a polynomial, 5x+6 and you want to integrate from 0 to 3 respect to x, how do you input in MATLAB? (I guess you can't figure out the constant once you integrate it since you don't have initial conditions. Ignore that constant part unless it's needed to execute the command.)

    My major problem is that the MATLAB doesn't want to execute anything unless I specify x prior to that, which means it doesn't want to work in variable forms.

    Bottomline, I want that equation integrated from 0 to 3 and substitute 100 into x to obtain value.

    Same as above, if I want to take a derivative of (x^2)+6 where x=2, what do I need to input in MATLAB?

    I've tried integrate, int, diff, and many other commands that I found under MATLAB but it doesn't do what I want it to do. (diff basically takes the difference but one source told me that it's for derivative.)

  2. jcsd
  3. Jan 22, 2010 #2
    If you don't actually need to produce the code, you can just plug it into www.wolframalpha.com. Also, to solve variables in MatLab, I believe you have to have certain packages installed. But:

    Type: syms x -this sets x as a symbolic variable
    Hit Return
    Type: int(5x+6,x,0,3)
    Hit return

    The problem you are probably having is you need to make sure you declare you variable as symbolic
  4. Jan 23, 2010 #3
    syms doesn't work...here's what message i get:

    ??? Undefined function or method 'syms' for input arguments of type 'char'.
  5. Jan 23, 2010 #4


    User Avatar
    Gold Member

    Which version of Matlab do you have? You need symbolic math module. Maybe you should try

    x = sym('x')
  6. Jan 23, 2010 #5
    i still get the same error and i am using 7.8.0 (R2009a). thanks!
  7. Jan 23, 2010 #6


    User Avatar
    Gold Member

    Type ver and tell me what the output is

    I'm getting (among many other things):

    Symbolic Math Toolbox Version 5.1 (R2008b)
  8. Jan 23, 2010 #7
    >> ver
    MATLAB Version (R2009a)
    MATLAB License Number: 542093
    Operating System: Microsoft Windows Vista Version 6.0 (Build 6002: Service Pack 2)
    Java VM Version: Java 1.6.0_04-b12 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
    MATLAB Version 7.8 (R2009a)
    Simulink Version 7.3 (R2009a)
    Bioinformatics Toolbox Version 3.3 (R2009a)
    Control System Toolbox Version 8.3 (R2009a)
    Curve Fitting Toolbox Version 2.0 (R2009a)
    Image Processing Toolbox Version 6.3 (R2009a)
    Instrument Control Toolbox Version 2.8 (R2009a)
    Optimization Toolbox Version 4.2 (R2009a)
    Signal Processing Blockset Version 6.9 (R2009a)
    Signal Processing Toolbox Version 6.11 (R2009a)
    SimMechanics Version 3.1 (R2009a)
    Simscape Version 3.1 (R2009a)
    Simulink Control Design Version 2.5 (R2009a)
    Stateflow Version 7.3 (R2009a)
    Statistics Toolbox Version 7.1 (R2009a)

    seems like i am missing that. it's not available from my school website. i am guessing this isn't free...?

    But, I think I should be able to solve this numerically if I input numbers to each variables, no?

    I've been manually integrate by hand and input it in MATLAB to find values, which totally defeats the purpose of using computer. :(
  9. Jan 23, 2010 #8


    User Avatar
    Gold Member

    Well mine was free, but I am not allowed to talk about it :rolleyes:
  10. Jan 23, 2010 #9


    User Avatar
    Science Advisor
    Gold Member

    Matlab isn't really a good package for symbolic math....

    Anway, to integrate numerically you can e.g. use the trapz function

    Try searching for "trapz" in the help-files (which, btw, are very good so you should learn to use them), note that the output of "help trapz" is very cryptic, but it you look in the help
    (the one that appears when you press F1 in Windows) you will find an example where they integrate sin(X)

    Generally speaking you need to define the intervall you want to integrate over using for example

    Code (Text):
    This will create an array with 100 elements ranging from 0 to 3 (the ";" is to prevent Matlab from printing the result in the command window)

    Then you define the function

    Code (Text):
    if you want you can now have a look at what this function looks like using

    Code (Text):

    to integrate using trapz you write

    Code (Text):
    "diff" works in more or less the same way, there is an example in the help.
  11. Jan 23, 2010 #10
    Thank you. I've looked in help, been to mathworks, etc.

    I am guessing the derivative should be done as follows for y=5x+6:

    a=[5 6];

    And I do understand it output coefficients in order from the lowest power to the highest. Thank you!
  12. Jan 24, 2010 #11
    Additional question,

    When you integrate polynomials, you get the constant, which can be identified based on initial condition. If you know the value for this constant, how do you specify it in MATLAB? Thanks
  13. Feb 25, 2011 #12
    hi , i think i can help you with your "syms" problem , i looked for it in mathworks and they said that the symbolic math tollbox isnt supported by 64-bit systems (for some matlab versions not all) , and they said that you can install the 32-bit version on a 64-bit system manually from the same matlab DVD , i havent tried it but i think its not that hard ....
    here is the mathwroks link to it :


    tell me if this works for you :)
  14. Mar 17, 2011 #13
    Please help me with matlab
    I have got a matlab program like this

    lambda= (WNBTIO21)*10^-9%m
    plot (B,D, 'or')
    grid minor;

    and i have another data
    how can I include these data in the program above to have both plot in a single axes?
  15. Mar 17, 2011 #14


    User Avatar
    Gold Member

    If you want both sets of data to be plotted on the same figure, use:
    Code (Text):
     hold on;
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook