Problem for doing Monte Carlo Integration

조회 수: 1 (최근 30일)
Deck Zhan Sim
Deck Zhan Sim 2021년 11월 23일
편집: Deck Zhan Sim 2021년 11월 23일
Hi all, I have a problem for doing the monte carlo integration.
Below here are all the codes, the %% Monte Carlo is the section for monte carlo integration:
%% Monte Carlo
monte_carlo=0; % Initialize Input
for i=1:h
monte_carlo=c*(sum(y)/h(i));
end
toc % Time Execution Stop
hold on
The outputs is looks something like this
I would be grateful that someone can help me to solve this matter. Thanks :)

채택된 답변

KSSV
KSSV 2021년 11월 23일
편집: KSSV 2021년 11월 23일
Your y in the ocde is a anonymous function:
function_handle with value:
@(x)sin(x)
You need to input some value into it....Like
y(1), y(2), y(pi) etc...
You cannot use sum(y) straight away.
May be you have to repalce this line:
monte_carlo=c*(sum(y)/h(i));
with
monte_carlo=c*(sum(y(x(1:i)))/h);
  댓글 수: 2
Deck Zhan Sim
Deck Zhan Sim 2021년 11월 23일
Thanks for helping, now I can get the answer that I want already.
KSSV
KSSV 2021년 11월 23일
But you need to improve the code. If h = 1, it will throw error.
Thanks is accepting/ voting the answer. :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by