MATLAB function help, input arguments errror

16 次查看(过去 30 天)
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 个评论
Adam Danz
Adam Danz 2019-4-29
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
Sonny Jordan 2019-4-29
Yes, that was a mistake. I tried doing Geometric as Steven asked me, but it still doesn't work.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-4-29
After saving your function, add it's path to matlab.
Suppose you saved your function in C:\Users\name\Documents\MATLAB\
addpath('C:\Users\name\Documents\MATLAB\')
Then you should have access to your function.
  2 个评论
Sonny Jordan
Sonny Jordan 2019-4-29
Thanks, it works now. But the function's answer is stuck on -0.5.
I did Geometric(0.5,0.5,1), but it gives me -0.5. If I type just Geometric, it still gives me -0.5. I tried Geometric(0.5,0.5,0), still gives me -0.5. Why's that?
Adam Danz
Adam Danz 2019-4-29
编辑:Adam Danz 2019-4-29
I'd need to look under the hood to speculated. If you want to copy-paste the entire function here I can look at it.
Is this the entire function?
function Probability = Geometric(p, q, x)
Probability = p*q^x-1;
When I run that function, with inputs of [0.5, 0.5, 1], I get an output of -0.75.
Furthermore, when you just type 'Geometric', you should get an error "Not enough input arguments".
Clear your workspace and try again.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2019-4-29
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 个评论
Sonny Jordan
Sonny Jordan 2019-4-29
I'm calling Geometric() from the command window, but after I created the function itself.
Adam Danz
Adam Danz 2019-4-29
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by