필터 지우기
필터 지우기

i get a wrong answer for my code, can someone please help!

조회 수: 2 (최근 30일)
Ibrahim
Ibrahim 2015년 1월 12일
댓글: Ibrahim 2015년 1월 12일
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 etc.
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 :-) )
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);
area = area+norm(cv*cv');
end
disp(area);

채택된 답변

Titus Edelhofer
Titus Edelhofer 2015년 1월 12일
Hi,
I'm not sure what you are doing in the second half of the function, but shouldn't the line
DTC(i)=norm(xvals(i))+norm(yvals(i));
read more like
DTC(i) = norm([xvals(i) yvals(i)]);
or
DTC(i) = sqrt(xvals(i).^2 + yvals(i).^2);
Titus
  댓글 수: 1
Ibrahim
Ibrahim 2015년 1월 12일
Thanks!
At first i had written: DTC(i) = sqrt(xvals(i).^2 + yvals(i).^2); but changeed it to norm.... after reading more about matlab (i am a new user) but yes there is a big difference between:
DTC(i)=norm(xvals(i))+norm(yvals(i)); and DTC(i) = norm([xvals(i) yvals(i)]);
that is corrected now! thanks for your time!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Julia
Julia 2015년 1월 12일
Hi,
I agree with Titus that the line
DTC(i)=norm(xvals(i))+norm(yvals(i));
looks strange. Did you mean abs() here?
And the lines below
%Inside the circle:
InC(i)=DTC(i)<=1;
do not test if the points are inside the circle.
Do you mean something like that:
If DTC(i)<=1
InC(i)=DTC(i)
end
?
  댓글 수: 2
Titus Edelhofer
Titus Edelhofer 2015년 1월 12일
Inc(i) = DTC(i)<=1; is a test for being inside. InC(i) is 1 if DTC(i)<=1, and zero else.
Titus
Ibrahim
Ibrahim 2015년 1월 12일
Thanks Julia!
it Works now!! :-D

댓글을 달려면 로그인하십시오.


Ibrahim
Ibrahim 2015년 1월 12일
mmm.. Guys?
now it writes the answer 3.2 in the command window, but not : ans = 3.2 in the workspace ? how come :/
  댓글 수: 2
Julia
Julia 2015년 1월 12일
A is your return value of the function, set
A=area;
and A should be stored in the workspace.
Ibrahim
Ibrahim 2015년 1월 12일
aaah! thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by