1. Not finding help here? Sign up for a free 30min 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!

Newbie question about matlab

  1. Feb 2, 2009 #1
    Hello!

    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( , ) ?

    F=@(x)x+3;
    G=@(x)tan(x);

    Cheers!
     
  2. jcsd
  3. Feb 3, 2009 #2

    MATLABdude

    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:
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/ezplot.html

    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:
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/trapz.html
     
  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

    Any idea?
     
  5. Feb 3, 2009 #4

    MATLABdude

    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).
    h=[f;g]'

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

    area(x,h)

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

    clear

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

    for i=1:k
    x=floor(6*rand(1,i)+1);
    y=floor(6*rand(1,i)+1);
    A=find((x+y)==6);
    q=length(A)/i;
    plot(i,q*100,'xr'),hold on
    end

    plot(1:k,30)

    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!

    clear

    x=0.:0.01:1.35;
    y1=tan(x);
    y2=x+3-tan(x);
    y=[y1' y2'];
    a=area(x,y)
    set(get(a(1),'Children'),'FaceColor','none')
    set(get(a(2),'Children'),'FaceColor','r')

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

    MATLABdude

    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:

    clear
    q=[];
    i=[];


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

    for i=1:k
    x=floor(6*rand(1,i)+1);
    y=floor(6*rand(1,i)+1);
    A=find((x+y)==6);
    q=[q, length(A)/i];
    end

    plot(i,q*100,'xr',i,100*mean(q))

    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.
    plot(i,q*100,'xr')
    line([1,k],[100*mean(q),100*mean(q)])

    Or some such.
     
    Last edited: Feb 3, 2009
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Newbie question about matlab
  1. Newbie python question (Replies: 4)

  2. Matlab question (Replies: 1)

Loading...