How to force Matlab/Octave cov function to use optional parameter - matlab

I'm using the cov (covariance) function of Matlab and Octave. Actually I'm using Octave, but in the end it has to work for both. This function has an optional second or third parameters to indicate whether normalization should be done with N or N-1.
If I do this: cov(points,1) (where points is 4x2 matrix) I get following error:
error: cov: x and y must have the same number of observations
On a general note I would like to know how is Matlab/Octave able to distinguish if the second parameter is another matrix or an optional parameter (because it can have 2 or 3 parameters).
More specifically I would like to know how can I solve my problem?
Matlab cov() documentation: http://www.mathworks.de/help/techdoc/ref/cov.html
Octave cov() documentation: http://www.gnu.org/software/octave/doc/interpreter/Correlation-and-Regression-Analysis.html
EDIT: I'm using Octave 3.2.4 on Ubuntu 12.04
EDIT2: The solution is to install a newer version of Octave. This features was implemented after 3.2.

This looks like an Octave bug (at least with version 3.0.5); it works fine in Matlab (at least with version 7.10).
Of course, to work around, you could just compute:
cov(a) * (N-1) / N

type help cov, and note that opt has been removed doesn't exist.
better yet, do
gedit /usr/share/octave/3.2.4/m/statistics/base/cov.m
and compare it to the old new code
Opt is gone recent. Use Oli's trick in the meantime
Edit: I take that back, this is a new feature. Not sure which version of Octave is using it, but you need an upgrade. Either, you do an unstable synaptic upgrade if you can find a repository, or you build it from source.
Edit 2: feature added Jan 2011 (3.4.1). 3.2.4 released Jan 2010. current version is 3.6.1

Related

Why MATLAB returns an error using `conv` function?

I'm using MATLAB 2015a. Even if I try to run document example of conv function, i get an error saying Error using conv (line 15), Not enough input arguments..
This is the example code I'm using:
u = [1 0 1];
v = [2 7];
w = conv(u,v)
What is the problem with my MATLAB?
It is hard to find the documentation for that version online without a license. You can find the documentation for your version by typing
help conv
Presumably the interface changed after your version. So you have to see what your documentation says.
FWIW, the documentation archives are here, but I cannot access them.
Also, I tried your code in Matlab 2015b (that is, b, not a), and it worked. So it must have changed between those two versions.
According to excaza, the docs haven't changed. So, like they say, it must be a shadowing issue. You would verify that by using clear all before your code snippet.

Why Maple generates unknown constant in the solution of PDE when all initial and boundary conditions are specified

I do not understand why Maple 2017.3, generates a solution
to this wave PDE with unknown constant in it called _C6.
As all boundary conditions and initial conditions are given.
Actually the solution is wrong on another account,
it gives divide by zero when n=2. But my question is on
the presence of this constant.
This is the solution for wave PDE u_tt = 4* u_xx. String. With
both ends fixed. Initial position is u(x,0)= sin(x)^2
and zero initial velocity i.e. u_t(x,0) = 0
Maple:
restart;
pde:=diff(u(x,t),t$2)= 4*diff(u(x,t),x$2);
bc:=u(0,t)=0,u(Pi,t)=0;
ic:=u(x,0)=sin(x)^2,D[2](u)(x,0)=0;
sol:=pdsolve([pde,bc,ic],u(x,t));
Maple 2017.3 on windows gives
There should not be _C constants in the solution. For reference, here is Mathematica solution
ClearAll[u,t,x,n];
pde=D[u[x,t],{t,2}]==4D[u[x,t],{x,2}];
ic={Derivative[0,1][u][x,0]==0,u[x,0]==Sin[x]^2}
bc={u[0,t]==0,u[Pi,t]==0};
sol=DSolve[{pde,bc,ic},u[x,t],{x,t}];
sol=sol/.K[1]->n (*n looks better than K[1] for index*)
Notice there is no constant in the solution (but Mathematica solution has
the same problem for n=2 as Maple's. Divide by zero. So its solution is also wrong.
But my question here is not on the n=2 issue, but on the constant _C6 that Maple generates, and why is it there?
Maple 2017.3 on windows.
Update
Just to confirm that latest physics package (thanks to the answer ) did fix this issue
If I download and install the latest revision for Maple 2017 of the Physics,DEs,MathFuncs Library then I get the following using Maple 2017.2 for 64bit Linux.
restart;
Physics:-Version();
"/usr/local/maple/maple2017.2/lib/Physics2017.mla", 2018, March 9, 23:54 hours
pde:=diff(u(x,t),t$2)= 4*diff(u(x,t),x$2):
bc:=u(0,t)=0,u(Pi,t)=0:
ic:=u(x,0)=sin(x)^2,D[2](u)(x,0)=0:
sol:=pdsolve([pde,bc,ic],u(x,t)):
lprint(sol);
u(x, t) = Sum(4*((-1)^n-1)*sin(x*n)*cos(2*t*n)/(Pi*n*(n^2-4)), n = 1 .. infinity)

Possible bug in `toc` function in Matlab R2015b

Usually calling a Matlab function with an empty comma-separated list is the same as calling it without arguments. For example,
in={}; spy(in{:});
is the same as
spy %// call without inputs
However, I'm getting strange behviour for the 1-output version of toc: this works as expected
tic; t=toc
whereas this crashes (closes Matlab) in R2015b, Windows 7 64 bits:
tic; in={}; t=toc(in{:})
It works properly in Matlab R2014b, Windows 7 64 bits.
Can someone reproduce this behaviour in R2015b? Any explanation? Possible bug?
Just to close this question:
From the comments above, this problem happens in Matlab R2015b in various operating systems. It doesn't happen in older Matlab versions.
I reported a bug to Mathworks. They told me be the developers are aware of this issue and it will be fixed for R2016b.
EDIT (October 3, 2016): I confirm that the bug has been corrected in R2016b.

One sample Kolmogorov-Smirnov to test gof of theoretical distribtion (makedist error) matlab

I have few continuous variables that look like this:
durs1=[3,40933 0,033630 0,25103 0,6361 0,71971 1,18311 1,91946 0,12842 0,97639 1,1383 0,46871 3,05241 2,34907 1,03788 0,76434 1,08798 1,462 0,4241 2,32128 0,29017..]
Each has more than 1000 values (all positive). I used
[a, b]=gamfit(durs1)
a =
2.3812 0.4200
b =
2.2316 0.3907
2.5408 0.4514
to find parameters of gamma distribution. Now I want to make a goodness of fit test in order to see how well the model fits my data. Matlab provides the one sample Kolmogorov-Smirnov test to solve the problem (http://www.mathworks.com/help/stats/kstest.html#btnyrvz-1)
But when I run my code (based on their examples):
test_cdf=makedist('Gamma','a',2.38,'b',0.42)
[h, p]=kstest(durs1,'CDF',test_cdf)
I have this error: "Undefined function 'makedist' for input arguments of type 'char'."
Can somebody help me to fix my code?
It seems like the function makedist of the statistics toolbox is available only from Matlab version r2013a. Looking in the documentation of earlier versions, even as late as r2012b, there is no mention of makedist. So I think updating to the latest version of matlab should solve your problem.

Values of exitflag in fminunc Matlab function is not consistant in Matlab 7.6 and 7.8

I need to know what exitflags mean in function fminunc, in Matlab version 7.6.
Specifically, I get value -2 which is not listed in
http://www.mathworks.com/help/toolbox/optim/ug/fminunc.html.
Thanks,
Farzane
That documentation is the latest one. Why don't you try looking inside the help that comes with your MATLAB version? That might contain info.
If not, enter type fminunc.m at the command line to see the source code and look there to see what -2 means.
Edit: This posting of the code of the function seems to indicate that any negative value indicates "FMINUNC did not converge to a solution"