Error when using Max command in a function

3 vues (au cours des 30 derniers jours)
Rachel Chambers
Rachel Chambers le 4 Sep 2017
Modifié(e) : Wonsang You le 4 Sep 2017
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 commentaires
the cyclist
the cyclist le 4 Sep 2017
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 le 4 Sep 2017
How are you calling this function? What are the data types and sizes of the variables?

Connectez-vous pour commenter.

Réponse acceptée

Wonsang You
Wonsang You le 4 Sep 2017
Modifié(e) : Wonsang You le 4 Sep 2017
Your function has no logical problem. Please make sure your argument y should be a numerical vector not logical.

Plus de réponses (0)

Catégories

En savoir plus sur Modeling dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by