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

1 回表示 (過去 30 日間)
Ibrahim
Ibrahim 2015 年 1 月 12 日
コメント済み: dpb 2015 年 1 月 13 日
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 件のコメント
Ibrahim
Ibrahim 2015 年 1 月 13 日
Hey Ced!
How do i write the vector xvals, without defining it? is it: xvals = [1:N]; ?
thanks for your time
dpb
dpb 2015 年 1 月 13 日
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.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by