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
Click For Summary

Discussion Overview

The discussion revolves around plotting functions and calculating probabilities in MATLAB, specifically focusing on plotting the area between two functions and simulating dice rolls to find the probability of rolling a sum of 6 with two dice. The scope includes technical explanations, mathematical reasoning, and programming challenges.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • Some participants suggest using the ezplot function to graph the functions x + 3 and tan(x), while others propose creating vectors for plotting.
  • There is a discussion about using the area function to fill the space between the two plotted functions, with suggestions on how to format the data correctly for this function.
  • One participant describes a MATLAB script for simulating dice rolls and calculating the probability of rolling a sum of 6, asking for help on how to visualize the mean value of the probabilities over multiple rolls.
  • Another participant provides a solution for plotting the area between the functions and discusses the need to avoid using the GUI for m-file scripts.
  • There are corrections and refinements to the initial code provided, with participants suggesting modifications to ensure the correct calculation and plotting of probabilities.

Areas of Agreement / Disagreement

Participants generally agree on the methods for plotting functions and calculating probabilities, but there are multiple approaches suggested for achieving the desired outcomes, and some uncertainty remains regarding the syntax and implementation details in MATLAB.

Contextual Notes

Some limitations include potential syntax errors in the proposed code snippets and the need for clarification on how to properly accumulate and visualize probability values over multiple iterations.

Who May Find This Useful

This discussion may be useful for individuals interested in MATLAB programming, particularly those working on mathematical modeling, simulations, or data visualization in the context of probability and statistics.

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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K