필터 지우기
필터 지우기

Approximation of e using random points

조회 수: 1 (최근 30일)
Austin
Austin 2011년 9월 9일
I have plotted one random thousand points on an axis. I have also plotted a line y=1/x. Now I am trying to display the points below the line in blue and the points above the line in orange. I have tried using an if loop but have not been successful. Any ideas how I could do this? Thanks
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 12월 8일
There is no such thing as an if loop.

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

채택된 답변

Rick Rosson
Rick Rosson 2011년 9월 9일
Hi Austin,
I have not tested the following code, so it may not be 100 percent correct, but it should give you the basic idea. I am assuming that you have two variables x and y, each one is a 1000 x 1 array of numbers:
idx = (y < 1./x);
figure;
scatter(x(idx),y(idx),'markerfacecolor','blue');
hold on;
scatter(x(~idx),y(~idx),'markerfacecolor','red');
HTH.
Rick

추가 답변 (7개)

Austin
Austin 2011년 9월 9일
Great! I had to make some slight adjustments to that, but it works. Thank you very much. I was stuck on the idea of using an if loop, but I guess that was unnecessary. However, I have now encountered another issue. I am trying to determine the percentage of points that lie below the curve. I am completely stuck on this. Any ideas?

Rick Rosson
Rick Rosson 2011년 9월 9일
Please try the following:
countBelow = sum(idx);
countTotal = size(x,1);
ratio = countBelow/countTotal;
HTH.
Rick

Austin
Austin 2011년 9월 9일
Thanks! Just curious, would there be a way to solve the initial question using either an if loop or a while loop? I was just so sure that would be the best way to do it, and I am curious if I was on the right path or not.

Rick Rosson
Rick Rosson 2011년 9월 9일
Yes, you can solve it with either a for loop or a while loop in conjunction with an if statement. So you were on a perfectly valid path. But in MATLAB, it is almost always possible (and usually desirable) to eliminate for loops and while loops, and replace them with what is called vectorized code (which is what the idx = ... line is doing). Some of the benefits of vectorization include:
  • Faster execution time
  • More readable code
  • More compact code
  • More expressive code
  • A higher level of abstraction
Also, in almost all programming languages, you generally want to avoid using if statements within loops if at all possible.
HTH.
Best,
Rick

Rick Rosson
Rick Rosson 2011년 9월 9일
BTW, were you able to compute a good value for e using this method? How close to the correct value did you get?

Austin
Austin 2011년 9월 9일
Well, I was able to get a value but it was not incredibly accurate. After running it a few times, the closest value i got was about 0.05 away from the actual value of e. However, I also want plot the approximated value of e vs. the number of iterations. This means I will need to have a loop, so that i can approximate e at each step along the way. Any help on how to do the loop? Thanks so much for your help.

Rick Rosson
Rick Rosson 2011년 9월 9일
Please post a new question on MATLAB Answers with a link to this one for background info.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by