Issues with MATLAB butter - matlab

I am using MATLAB R2015 and cannot implement 'butter' without getting an error related to too many output arguments from the polyfit part of the script. I have used the same implementation for years with other versions of MATLAB.
Example:
[b, a] = butter(2,[.15,.3]);
Error using poly
Too many output arguments.
Error in zp2ss (line 127)
den = real(poly(p(i:i+1)));
Error in butter (line 97)
[a,b,c,d] = zp2ss(z,p,k);
I get the same error implementing the examples in the help documentation.

Just a guess but is there any chance you've defined your own poly function? which('poly') should point to some Matlab directory unless you've defined it elsewhere, potentially as a variable?. For me on a newer version that path is something like $MATLAB/toolbox/matlab/polyfun/poly.m You can also edit the poly function edit poly and verify that there is one output argument for the function.
Also, I'd advise against ever writing decimal numbers without a leading zero. It took me way to long to figure out what [.15,.3] was. Instead write [0.15,0.3] or even just [0.15 0.3] Edit: I just realized that is an example in Matlab ... my point stands but the toolbox author should know better ...

Related

How to get the zeros of the given equation using fzero in MATLAB?

I have the following function that I wish to solve using fzero:
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)}
Here, C, T_m, T_i, and L_f are all input by the user.
On trying to solve using fzero, MATLAB gives the following error.
Undefined function or variable 'X'.
(where X are the variables stated above)
This error is understandable. But is there a way around it? How do I solve this?
This is answered to the best of my understanding after reading your question as it's not really clear what you are exactly trying and what you want exactly.
Posting the exact lines of code helps a big deal in understanding(as clean as possible, remove clutter). If then the output that matlab gives is added it becomes a whole lot easier to make sure we answer your question properly and it allows us to try it out. Usually it's a good idea to give some example values for data that is to be entered by the user anyway.
First of to make it a function it either needs a handle.
Or if you have it saved it as a matlab file you generally do not want other inputs in your m file then the variable.
So,
function [out]=yourfun(in)
constants=your values; %you can set a input or inputdlg to get a value from the user
out= something something, your lambda thingy probably; %this is the equation/function you're solving for
end
Now since that is not all that convenient I suggest the following
%declare or get your constants here, above the function makes it easier
syms lambda
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)};
hf=matlabFunction(f); %this way matlab automatically converts it to a function handle, alternatively put #(lambda) in front
fzero(hf,x0)
Also this matlab page might help you as well ;)

MATLAB: using a minimum function within symsum

I am trying to run code similar to the following, I replaced the function I had with one much smaller, to provide a minimum working example:
clear
syms k m
n=2;
symsum(symsum(k*m,m,0,min(k,n-k)),k,0,n)
I receive the following error message:
"Error using sym/min (line 86)
Input arguments must be convertible to floating-point numbers."
I think this means that the min function cannot be used with symbolic arguments. However, I was hoping that MATLAB would be substituting in actual numbers through its iterations of k=0:n.
Is there a way to get this to work? Any help much appreciated. So far I the most relevant page I found was here, but I am somewhat hesitant as I find it difficult to understand what this function does.
EDIT following #horchler, I messed around putting it in various places to try and make it work, and this one did:
clear
syms k m
n=2;
symsum(symsum(k*m,m,0,feval(symengine, 'min', k,n-k)),k,0,n)
Because I do not really understand this feval function, I was curious to whether there was a better, perhaps more commonly-used solution. Although it is a different function, there are many pieces online advising against the eval function, for example. I thought perhaps this one may also carry issues.
I agree that Matlab should be able to solve this as you expect, even though the documentation is clear that it won't.
Why the issue occurs
The problem is due the inner symbolic summation, and the min function itself, being evaluated first:
symsum(k*m,m,0,min(k,n-k))
In this case, the input arguments to sym/min are not "convertible to floating-point numbers" as k is a symbolic variable. It is only after you wrap the above in another symbolic summation that k becomes clearly defined and could conceivably be reduced to numbers, but the inner expression has already generated an error so it's too late.
I think that it's a poor choice for sym/min to return an error. Rather, it should just return itself. This is what the sym/int function does when it can't evaluate an integral symbolically or numerically. MuPAD (see below) and Mathematica 10 also do something like this as well for their min functions.
About the workaround
This directly calls a MuPAD's min function. Calling MuPAD functions from Matlab is discussed in more detail in this article from The MathWorks.
If you like, you can wrap it in a function or an anonymous function to make calling it cleaner, e.g.:
symmin = #(x,y)feval(symengine,'min',x,y);
Then, you code would simply be:
syms k m
n = 2;
symsum(symsum(k*m,m,0,symmin(k,n-k)),k,0,n)
If you look at the code for sym/min in the Symbolic Math toolbox (type edit sym/min in your Command Window), you'll see that it's based on a different function: symobj::maxmin. I don't know why it doesn't just call MuPAD's min, other than performance reasons perhaps. You might consider filing a service request with The MathWorks to ask about this issue.

Error regarding inlineeval in MATLAB

As part of a group project we have a system of 2 non linear differential equations and we have to draw the S=S(t) , I=I(t) graphic using the midpoint method.
And I'm getting the following error when trying to insert the matrix with the corresponding differential equations:
"Error in inline expression ==> matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]])
Undefined function 'matrix' for input arguments of type 'double'.
Error in inline/subsref (line 23)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);"
The code I have done is the following:
syms I S
u=[S;I];
F=[-0.001*S*I;0.001*S*I-0.3*I];
F1=inline(char(F),'I','S');
h=100; %Valores aleatórios
T=100000;
ni=(T/h);
u0=[799;1];
f=zeros(1,2);
k=zeros(1,2);
i=1;
while i<=ni
f(1)=F1(u0(1));
f(2)=F1(u0(2));
dx=h*f;
k(1)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));
k(2)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));
u1=u0+h*k;
disp('i:'),disp(i)
disp('u= '),disp(u1)
u0=u1;
i=i+1;
end
I'm new to this so the algorithm it's very likely to be wrong but if someone could help me with that error I'd apreciate it. Thank you!
The problem that specifically creates the error is that you are putting two symbolic functions into a matrix and then calling char (which outputs matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]]) rather than converting nicely to string).
The secondary problem is that you are trying to pass two functions simultaneously to inline. inline creates a single function from a string (and using anonymous functions instead of inline is preferred anyway). You cannot put multiple functions in it.
You don't need sym here. In fact, avoid it (more trouble than it's worth) if you don't need to manipulate the equations at all. A common method is to create a cell array:
F{1} = #(I,S) -0.001*S*I;
F{2} = #(I,S) 0.001*S*I-0.3*I;
You can then pass in I and S as so:
F{1}(500,500)
Note that both your functions include both I and S, so they are always necessary. Reconsider what you were expecting when passing only one variable like this: f(1)=F1(u0(1));, because that will also give an error.

sym2poly selectively working matlab

I'm evaluating a series of theoretical (not necessarily functional) circuits in matlab. I have been trying to get the transfer function of the circuits and during the process I use the sym2poly function. Sometimes, sym2poly works and returns the transfer function. Sometimes it does not.
This is what the code looks like:
[n,d] = numden(eval(v_3/V));
transH = tf(sym2poly(n),sym2poly(d))
n and d are symbolic cell arrays. The error I get is:
Error using sym/sym2poly (line 28)
Not a polynomial.
Error in CircuitGA (line 349)
n = sym2poly(n);
This looks similar to several questions posted a long time ago, but all of those were solved by a bug fix in an updated version of the symbolic math toolbox. Does it mean that what I am giving it is impossible to turn into a polynomial?
Is there a fix?
Any suggestions of a method that will work for all my circuit arrays?
Maybe a try and catch for if it can return a transfer function?

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.