1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Newbie question about matlab

  1. Feb 2, 2009 #1

    Its my first post here and I think I found the right category :)

    How to I plot the area between x + 3 and tan(x), 0<x<1.4 in matlab?
    And can I somehow recall those functions in plot( , ) ?


  2. jcsd
  3. Feb 3, 2009 #2


    User Avatar
    Science Advisor

    I don't understand. Are you trying to plot both x+3 and tan(x)?, or just their difference, x+3 - tan(x)?

    You could use ezplot to graph these:

    But it's better to create a vector of values e.g. x=[0:0.1:1.4] (creates a bunch of values between 0 and 1.4, in 0.1 increments), and then do something like f=x+3, which will create a vector f which adds 3 to all the values in x. g=tan(x) will, of course, create a vector g which just takes the tan of all your x values.

    Then you can use plot(x, f, x, g) to plot these multiple functions. As to how to highlight the area between them, well, that's probably just using some of the graphical features in the plot window.

    Now if you're actually trying to calculate the area between these functions, take a look at the trapz (for trapezoid rule) function:
  4. Feb 3, 2009 #3
    Thank you for your time. I´ve tried the area(, )-function but I just dont get it right.

    I would like to have it like this:
    http://img98.imageshack.us/img98/4197/fillingre3.jpg [Broken]

    Any idea?
    Last edited by a moderator: May 4, 2017
  5. Feb 3, 2009 #4


    User Avatar
    Science Advisor

    I think I see the problem. Area expects a 1xN vector for the first entry, and an NxM matrix for your stackup. Try doing the following (after having made x, f, and g).

    (this makes a 2xN matrix from f and g, and then transposes it). Then use the area function:


    If you edit the graph (tools > edit plot), and then delete the bottom portion, presto, a filled in space between the two functions.
  6. Feb 3, 2009 #5
    Yes that´s one way of doing that. But the thing is I have to handle in a m-file for it, and need to do it without the GUI (not allowed to convert m-files that way). It cant be that difficult :)

    I have another thing where I some help tho.
    I´ve created a little program that calculates the probability of gain a total number of 6 with two dices. (6+0, 5+1,4+2,3+3 etc..).


    k=input('How many times would you like to throw your dices? ');

    for i=1:k
    plot(i,q*100,'xr'),hold on


    Everything is fine so far, but now I´d like to add a line where I can se the mean-value of all throws, were 30 is right now. So I need to sum all q-values but I dont get that right. Do you have any idea how I can do that?

    Thank you very much for you help, I appreciate that !
  7. Feb 3, 2009 #6
    First one i solved!


    y=[y1' y2'];

    But I still need some help with the 2nd one :/
  8. Feb 3, 2009 #7


    User Avatar
    Science Advisor

    Excellent! Well, I think your problem is just in how you construct your plot (using hold instead of building up a vector).

    doing q=[q, 1] will concatenate 1 onto the end of q. So, if you slightly modify your script to:


    k=input('How many times would you like to throw your die? ');

    for i=1:k
    q=[q, length(A)/i];


    I don't know if that last line contains a syntax error, but it's fixable. I think that gets you what you want. Or, you could keep most of what you had previously, but just create a value called qSum that adds the current value of q to itself every time the loop runs.

    EDIT: oops, that line *does* contain an error.

    Or some such.
    Last edited: Feb 3, 2009
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook