MATLAB function help, input arguments errror
Show older comments
I'm using matlab, and I'm trying to make a function that gives me the probabilities in a geometric distribution when I enter the values of p, q, and no. of attempts(x) as the inputs.
My function:
function Probability = Geometric(p, q, x)
Probability = p*q^x-1
Now whenever I try to calculate the probability by typing in the values of p, q, and x, such as:
geometric(0.5, 0.5, 1),
it gives me an error about "invalid function for input arguments of type double" sort. Can anyone please help me?
I've tried changing functions, and reducing them to one input and one output.
Please help me someone, and if possible, give me a recommendation on how to make a better function. I thought on making one on the binomial distribution, but it would be difficult to get factorials, and get the probabilities to sum up if I need probabilties for multiple succesess.
9 Comments
Adam Danz
on 29 Apr 2019
It would be helpful to have
- the full copy-pasted error message
- all relevant code that defines the function and it's inputs
Sonny Jordan
on 29 Apr 2019
Adam Danz
on 29 Apr 2019
See Steven Lord's answer below. If changing the case from geometric() to Geometric() fixed the problem, please accept his answer.
Sonny Jordan
on 29 Apr 2019
First of all, we need to know which of the two functions you're trying to reference:
- Geometric()
- geometric()
Then we need to know where you're storing this function. Is it in it's own m-file with the same exact case-sensitive name? Is that funciton on the matlab path? Is it a nested function?
After we've got answers to those question we can take some additional steps as needed.
Sonny Jordan
on 29 Apr 2019
Adam Danz
on 29 Apr 2019

Adam Danz
on 29 Apr 2019
As Steven Lord wrote 30 minuts ago, you're not calling Geometric(); you're calling geometric() which is not the same function. At least that's what you've told us in your question. Maybe it was a typo but we can only work with the info that was given to us.
Sonny Jordan
on 29 Apr 2019
Accepted Answer
More Answers (1)
Steven Lord
on 29 Apr 2019
function Probability = Geometric(p, q, x)
This suggests the name of the function file containing this function is Geometric.m.
geometric(0.5, 0.5, 1),
The function named geometric is not the same as the function named Geometric. Function names in MATLAB are case sensitive.
If changing the case of your call does not resolve the problem, please provide the information Adam Danz requested. Don't paraphrase the error message, copy all the red text directly from the Command Window into the MATLAB Answers comment box.
7 Comments
Sonny Jordan
on 29 Apr 2019
Ok, so you haven't got a function named Geometric() nor a function named geometric(), is that correct? You can confirm that by running these two lines:
which Geometric
which geometric
Sonny Jordan
on 29 Apr 2019
Ok, this is helpful. So matlab doesn't know the function 'Geometric()' exists. You also mentioned that it's a nested function. Are you calling Geometric() from within the same file that defines this function? Are you calling Geometric() from the command window before the function was created?
Sonny Jordan
on 29 Apr 2019
Sonny Jordan
on 29 Apr 2019
Adam Danz
on 29 Apr 2019
I thought you mentioned that it was a nested function earlier but I may have misread your comment. So you have a file named 'Geometric.m' and the file is saved and the function within that file has the same name. In that case, it sounds like you just need to add the path to matlab. I've provided an answer to show you how to do that.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!