code should give 3.2 but i get 0.0729, is there an easier way doing this code?

Hey everybody!
(see Attached file in the hyperlink: http://www.docdroid.net/ozso/exercise-matlab-1.pdf.html )
I need to calculate an estimated value of the circle area. Giving some x coordinates and y coordinates. i Have written the following code, but there is something wrong with it. can test my code by the test script: circleAreaMC([-0.1, 0.7, 0.8, 0.5, -0.4], [0.3, -0.1, 0.9, 0.6, -0.3]) and it should give 3.2. But it gives 0.303.
I asked this question before, but i try Again now since i got another wrong answer: ans = 0.0729
I really feel like this problem have a much easier solution, can somebody please help me? I am new to matlab. (started 5 days ago :-) )
Script is as follows:
function A = circleAreaMC(xvals,yvals)
%A function that estimates the area of a circle
% by Monte Carlo simulation.
N=5;
InC = zeros(size(xvals));
DTC = zeros(size(xvals));
%A loop that test wether or not the points are inside the circle:
for i=1:length(xvals)
%Distance to center:
DTC(i)=norm(xvals(i))+norm(yvals(i));
%Inside the circle:
InC(i)=DTC(i)<=1;
end
%the 2 vectors:
xvals = (xvals.*InC(i));
yvals = (yvals.*InC(i));
xvals(xvals==0)=[];
yvals(yvals==0)=[];
x = zeros(size(xvals));
y = zeros(size(xvals));
%for loop:
for n=2:length(xvals)
x(n)=xvals(n)-xvals(1);
y(n)=yvals(n)-yvals(1);
end
x(x==0)=[];
y(y==0)=[];
area = 0;
for q = 1:(length(x)-1)
v = [x(q);y(q);0];
v1 = [x(q+1);y(q+1);0];
cv = cross(v,v1);
A = area+norm(cv*cv');
end
Thanks for your time!

10 Comments

basically i need the following help:
How do i give matlab a "random number" of x and y coordinates, and expect it to only return thoose points that are Within the circle radius.
To test if a point (x, y) is inside the circle, we can simply check if it magnitude of the vector from the center of the circle to (x, y) is less than one.
please ! i will appreciate any help given!
The "Matlab" way might be to
a) generate the sample vectors x and y of length N
b) compute the length of each pair as a new vector
c) sum the logical (r<=R) where r is the radius vector and R is limit
d) compute A as that number over number of trials times the rectangle area
No loops needed...
if i only do this without any other condition, the code will be like Rick Rossons code, and that code, gives me 4 different answers each time i run it in matlab. For N = 5, and for circleAreaMC([-0.1, 0.7, 0.8, 0.5, -0.4], [0.3, -0.1, 0.9, 0.6, -0.3]) matlab returns one of following 4, 1.6, 3.2, 2.4. do you know why?
i cannot give a condition like: 3 < A < 4, because i have to run the script with different ammmount of numbers and that returns a different value each time outside that range!
thanks for your time!
The link to the exercise says as instruction/background specifically "...N random points uniformly on the the circumscribing square and counting how many points n that fall within the circle, we can estimate the area of the circle by multiplying the area of the square by the fraction of points inside the circle..."
That is what the outline I gave above does but NOT at all what your code attempts to do.
Remove the constant N=5; use length(x) to determine the size of the passed vectors so the function is independent.
Check each step; are you sure they're actually computing what you think they are correctly? Particularly look at the formulation you have to determine the length and see what that is actually returning as you have written it.
Once you've worked those details out, again I'd note you don't need any loops at all.
hmm.. by: a) generate the sample vectors of length N. is it just writing: x = [-0.1, 0.7, 0.8, 0.5, -0.4]; y = [0.3, -0.1, 0.9, 0.6, -0.3]; or writing : x = [5:N[; ?
i understand step c and d, but not a and b :/ i'm sorry! i am a new user of matlab.
function A = circleAreaMC(xvals,yvals)
N = 5;
xvals = [-0.1, 0.7, 0.8, 0.5, -0.4];
yvals = [0.3, -0.1, 0.9, 0.6, -0.3];
r = sqrt(xvals.^2 + yvals.^2);
n = sum(r <= 1);
A = 4*n/N;
end
This code Works, but what do i write to change the vectors so that they can "take form" of any value and length?
thanks for your time!
First of all... why do you define xvals and yvals in your function if you pass them as parameters?
And to generalize it, you need to get rid of any point-specific values. That would include defining xvals, yvals, and N in your code. These values need to be extracted from the inputs.
But to be honest, it would make more sense to me to write the function as
function A = circleAreaMC(N)
and then generate N uniformly distributed values for x and y in the function itself. You can do this using "rand".
Hey Ced!
How do i write the vector xvals, without defining it? is it: xvals = [1:N]; ?
thanks for your time
doc rand
I think you need to type
doc
at the command line and just start through the tutorial information under the "Getting Started" tab to begin to learn the basics of Matlab syntax and use. This takes you through the basics of syntax and using arrays and "how Matlab works" to show the concepts.

Sign in to comment.

Answers (0)

Categories

Asked:

on 12 Jan 2015

Commented:

dpb
on 13 Jan 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!