Error in Matlab code using rand function - matlab

I have been using the following matlab code to generate random numbers,
rand('state',0);
n=100;
m=300;
A=rand(m,n);
b=A*ones(n,1)/2;
c=-rand(n,1);
However, it keeps giving me the message "Error in ESE605_Homework_4 (line 1)
rand('state',0)'"I have tried to evaluate the problem, and it says I should switch to using the "rng" function because newer versions of Matlab discourage use of "rand". However, upon switching to the rng, function I am still having issues. Can anyone offer some help?

From Matlab documnetaion
for 'state' option you should use rng function instead of rand

Related

Laguerre polynomials in MATLAB

I tried using the .. command in MATLAB to generate Laguerre polynomials but I keep getting this error every time:
I found this in the help section:
Since I have defined x as symbolic I shouldn't be getting this error.
Also on website I found this which says that the function does not run in MATLAB.
Can anyone help? Thanks in advance
Like you say, and the matlab help says this function only works inside mupad, maybe in later versions it works in matlab console.
If you want to use it, write mupad in Matlab Command window and then use it in the mupad, matlab will return you the result as I show in the picture
In R2014b+, there is a laguerreL function available directly from within Matlab. However, a version of this function was introduced to MuPAD in R2009a. You can call the MuPAD version from within Matlab
syms x;
feval(symengine,'laguerreL',2,x)
or
evalin(symengine,'laguerreL(2,x)')
Both return x^2/2 - 2*x + 1.
You can read more about interacting with MuPAD functionality from Matlab here. However, I'd recommend browsing and searching the archived documentation for your specific version or using your built-in HTML documentation (e.g., doc mupad or doc 'calling mupad').

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?

Matlab table function

I have tried running the example as shown in the documentation of MuPAD table:
T := table(a = 13, c = 47)
Doing so gives me the following error:
Undefined function 'T' for input arguments of type 'char'.
I have no idea what is going on. Does anybody know why it isn't working and how I can create a table using this function?
If you look at the top of the page, you are reading about functions contained in the "Symbolic Math Toolbox." I believe you need to pay for that license to use functions from the toolbox.
From Matlab's help for that error you either:
Made a typographical error ...
Changed directories so that a function is no longer on the search path...
Used the wrong case for a function or variable name ...
Are trying to use a function for which you are not licensed.
I got the same error as you, and I also do not have that Toolbox, so perhaps its the last reason.
Is it time to switch to Python?
This happens when you try to call a muPAD function from the MATLAB command line. Whenever you see :=, that's a clue that you're dealing with muPAD. You cannot use muPAD syntax directly in MATLAB (feval or evalin and symengine can be used in some cases to call muPAD functions and return a symbolic expression).
To use table in muPAD:
Call mupad at the command line to open up a muPAD notebook, then call your sample line. You do not need the latest MATLAB version, although I'm not sure exactly when it was brought in (works fine for me on 2011b with Symbolic toolbox).
To use table in MATLAB:
The muPAD table function should not be confused with the MATLAB table function/datatype, which is relatively new. The equivalent in MATLAB of that muPAD code would be something along the lines of (untested):
T = table([13;47],'RowNames',{'a';'c'});

Solving Bessel Function using Runge Kutta

I'm working on an assignment for a class of mine and I'm supposed to write a code using a program of my choice (I've chosen Matlab) to solve the Bessel function differential equation using the 4th order Runge-Kutta method. For reference the Bessel function DE is:
x^2*(J_n)''+x*(J_n)'+(x^2-n^2)*J_n=0.
I'm able to separate this into two coupled first order DEs by:
(J_n)'=Z_n and
(Z_n)'+(1/x)*Z_n+[(x^2-n^2)/x^2]*J_n=0.
I have no experience with Matlab nor any other programming language before this assignment. I know Matlab has the 'ode45' command but I'm supposed to write the code myself, not rely on Matlab's commands. So far I've been working on the n=0 case for the Bessel function but I keep getting an error when I try and plot the function. The current error I have says: "Undefined function or method 'J' for input arguments of type 'double'." But I don't know how to fix this error nor if my code is even correct. Could someone tell me where I've gone wrong or what is the correct way to write this code?
h=0.01; %step size
J_0(1)=1; %initial condition for J_0
Z_0(1)=1; %initial condition for Z_0-This value should be zero
%but Matlab gives me an error. To fix this, I input
%Z_0(1)-1 to use the correct value for Z_0(1).
x(1)=0.001; %first value of x
dZ(Z_0,J_0)=(-1/x)*(Z_0-1)-J_0;
for i=[1:1:10]
dZ1=(-1/x)*(Z_0-1)-J_0;
dJ1=(Z_0(1)-1)*h;
dZ2=(-1/x)*(Z_0-1+0.5*h)-(J_0+0.5*h*dJ1);
dJ2=((Z_0(1)-1)+dZ1)*h;
dZ3=(-1/x)*(Z_0-1+0.5*h)-(J_0+0.5*h*dJ2);
dJ3=((Z_0(1)-1)+dZ1+dZ2)*h;
dZ4=(-1/x)*(Z_0-1+h)-(J_0+h*dJ3);
dJ4=((Z_0(1)-1)+dZ1+dZ2+dZ3)*h;
J(i+1)=J(i)+(h/6)*(dJ1+2*dJ2+2*dJ3+dJ4);
end
plot(J_0);
Thanks in advance for any help
Your problem is on the line:
J(i+1)=J(i)+(h/6)*(dJ1+2*dJ2+2*dJ3+dJ4);
In the right-hand side of your assignment operator you use the variable J that is never set before i is taking the value 1. Looks like a typo to me (should it be J_0 instead?)
Also, don't forget your index i when computing your dJ and dZ stuff in the for loop.

adftest function error in lagmatrix

Using the adftest function in MATLAB's econometrics toolbox, I'm receiving the following error:
>> [h1,pVal1] = adftest(y1,'model','ARD')
Error using lagmatrix (line 25)
lagmatrix: wrong # of input arguments
Error in adftest>runReg (line 705)
yLags = lagmatrix(y,0:(testLags+1));
Error in adftest (line 417)
testReg = runReg(i,y,testT,testLags,testModel,needRegOut);
y1 is a <41x1> vector of doubles.
Has anyone received this error or have any thoughts on what the issue is? I am using this code right out of the box so I'm not sure what is going on. I'd post to MATLAB's site, but it is down for maintenance.
This is either a bug in Matlab, in which case you should submit it on the Matlab support site. Before you do that, you should check that you don't have a function lagmatrix on your path that shadows the built-in function. Type
which lagmatrix
on the command line. If the path does not point to your Matlab installation, you should move lagmatrix off the Matlab search path.
Also note that y1 should not contain all NaN, or be otherwise degenerate, so you may want to check the function using the sample data as suggested in the help to be sure it's a bug and not just your data.
I had the same problem with this function. In my case, the problem was the function lagmatrix (older version) in my MATLAB path and the adftest function was the newest version. The soluction was delete the older version of lagmatrix.