필터 지우기
필터 지우기

Error when using Max command in a function

조회 수: 2 (최근 30일)
Rachel Chambers
Rachel Chambers 2017년 9월 4일
편집: Wonsang You 2017년 9월 4일
When creating a function that performs a Monte-Carlo simulation I have to find the maximum value in an array, but when I try this I get an error code that says: Error using max Invalid data type. First argument must be numeric or logical.
Here is my function:
function [ integral_approx ] = montecarlosim( a,b,y,n )
x=[a,b];
r=max(y);
v=(max(x)-min(x)).*rand(n,1)+min(x);
w=(r).*rand(n,1);
x=v;
i=find((w>=0)&(w<=y));
m=numel(i);
(m/n)*r*(b-a)
end
  댓글 수: 2
the cyclist
the cyclist 2017년 9월 4일
Your function executes to completion for me, for a variety of inputs. For example
montecarlosim(1,2,3,4)
executes for me.
How are you calling this function?
James Tursa
James Tursa 2017년 9월 4일
How are you calling this function? What are the data types and sizes of the variables?

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

채택된 답변

Wonsang You
Wonsang You 2017년 9월 4일
편집: Wonsang You 2017년 9월 4일
Your function has no logical problem. Please make sure your argument y should be a numerical vector not logical.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by