How can I plot the probability of rolling a sum of 6 on two dice in MATLAB?

  • Thread starter Thread starter rularn
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion focuses on plotting the area between the functions x + 3 and tan(x) in MATLAB, with users sharing methods to achieve this. One user suggests creating vectors for the functions and using the plot function, while another offers a solution using the area function to fill the space between the curves. A separate topic arises regarding calculating the probability of rolling a sum of 6 with two dice, where users provide code snippets to visualize the results. The conversation includes troubleshooting and refining the code to display the mean value of the probability over multiple rolls. Overall, the thread emphasizes practical coding solutions in MATLAB for both plotting functions and probability calculations.
rularn
Messages
8
Reaction score
0
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!
 
Physics news on Phys.org
rularn said:
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!

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
 
MATLABdude said:
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

Thank you for your time. I´ve tried the area(, )-function but I just don't get it right.

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

Any idea?
 
Last edited by a moderator:
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.
 
MATLABdude said:
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.

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 can't 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 don't get that right. Do you have any idea how I can do that?

Thank you very much for you help, I appreciate that !
 
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 :/
 
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:
Back
Top