??? Input argument "x" is undefined - matlab

I enter:
EDU>> %using the temporary variable levels
EDU>> levels=range/quantise_range;
levels=round(levels);
quantisation_bits=log2(levels)
NB. There is no x variable anywhere
My error is:
??? Input argument "x" is undefined.
Error in ==> range at 18
y = max(x) - min(x);
EDU>> %combining above process into one statement
quantisation_bits=log2(round(range/quantise_range));
??? Input argument "x" is undefined.
Error in ==> range at 18
y = max(x) - min(x);
Would someone care to explain thie issue? I am beginner into programming and I really don't understand how to read the error hint.
Thanks.
Additionally, in what may be due to whatever the same principle misunderstanding on my part is, I am finding trouble here with this code, with error also included:
%Trying to create my own function, I’m pressing shift+enter at the end of lines for neatness:
EDU>> function what_am_i()
disp 'I am a function'
??? function what_am_i()
|
Error: Function definitions are not
permitted in this context.
EDU>>
function what_am_i()
disp' I am a function'
??? function what_am_i()
|
Error: Function definitions are not
permitted in this context.
EDU>> end
??? end
|
Error: Illegal use of reserved keyword
"end".

You may be confusing the "Command Window" with the "Editor".
In the "Command Window" you can enter some lines of code, but can't create functions. It acts more like a calculator.
If you create and save files, then you open them in the "Editor" and that's when Matlab begins behaving more like a programming language. It saves .m files that can have functions or algorithms written in them.
Matlab has lots of help available. I recommend visiting their website and searching around a bit. (or just google "intro to matlab")

In addition to #user1860611's answer regarding function definitions in the command window, the other problem you have has to do with range, which is a built-in function. It appears you are trying to use range it as a variable name, but didn't actually initialize it to a value, so it is still a function.
In the line here:
levels=range/quantise_range;
you are essentially calling the range function, but without passing it an argument.
Error in ==> range at 18
y = max(x) - min(x);
The error message is telling you that a function called range has generated an error. It doesn't matter that you don't have a variable named x, because the function has one internally.

Related

About symbolic MATLAB

What is wrong with the following code?
clear all
syms x y a ;
u=2*x*y;
v=a^2+x^2-y^2;
diff=diff(u,'x',1)+diff(v,'y',1);
if diff==0
disp('Liquid motion is possible.')
disp('Stream function exists.')
else
disp('Liquid motion is not possible.')
disp('Stream function does not exist.')
end
diff2=diff(v,'x',1)-diff(u,'y',1);
if diff2==0
disp('Velocity potential exists.')
else
disp('Velocity potential does not exist.')
end
This comes in the command window when I run the above.
Liquid motion is possible.
Stream function exists.
Error using sym/subsindex (line 672)
Invalid indexing or function definition. When defining a function, ensure that the body of the function is a SYM
object. When indexing, the input must be numeric, logical or ':'.
Error in sym>privformat (line 1502)
x = subsindex(x)+1;
Error in sym/subsref (line 694)
[inds{k},refs{k}] = privformat(inds{k});
Error in q7 (line 17)
diff2=diff(v,'x',1)-diff(u,'y',1);
But if I rewrite(redefine) the symbolic variables after the first if construct, it runs fine. Also if I cancel the first if construct, it runs.
I would avoid to overwrite a reserved name, so instead of
diff=diff(u,'x',1)+diff(v,'y',1);
I would suggest
derFcn = diff(u,'x',1)+diff(v,'y',1);
This triggers the second error;
diff2=diff(v,'x',1)-diff(u,'y',1);
at this point diff is your diff value (which, incidentally is 0) so it is equivalent to write
0(v,'x',1)
which, of course, will not compile and it is not what you meant.
So, please, make the substitution (and update your if statements accordingly).

Using fzero to solve eqn in MatLab

I hope this is the right area. I'm trying to get this code to work in MatLab.
function y=test(x)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
I then jump to the command value and type this:
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
I then try to find the zeros of the first equation by typing this and I get errors:
solution=fzero(#test,5000000)
I'm getting the following error:
Error: File: test.m Line: 5 Column: 1 This statement is not
inside any function. (It follows the END that terminates the
definition of the function "test".)
New error
Error using fzero (line 289)
FZERO cannot continue because user supplied function_handle ==> #(x)
(test(x,B,b,a,r,p))
failed with the error below.
Subscript indices must either be real positive integers or logicals.
I would guess that this is a problem of scoping, you are defining variables (B, b, etc...) in the command line but trying to use them inside your test function where they are out of scope. You should alter your test function to take these in as parameters and then use an anonymous function so that your call to test in fsolve still only takes a single parameter:
function y=test(x, B, b, r, a, p)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
and
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
solution=fzero(#(x)(test(x,B,b,a,r,p)),5000000)
As an aside, unless you really do mean matrix multiplication, I would suggest that you replace all your *s and /s in test with the element-wise operators .* and ./. If you are dealing with scalars, it doesn't matter now, but it makes a big difference if you later want to scale your project and need a vectorized solution.
Regarding the errors you have added to your question:
You can't put code after the end in your function file. (With the exception of local functions). Your objective function should be an .m-file containing the code for one single function.
This is because in your test function you have ...b((1-(b/x)^(B-1))... which in MATLAB means you are trying to index the variable b in which case the value of (1-(b/x)^(B-1) has to be a positive integer. I'm guess you are missing a *
Your
function y=test(x)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
cannot access variables in your workspace. You need to pass the values in somehow. You could do something like:
function y=test(x,B,b,a,r,p)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
and then you can create an implicit wrapper function:
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
solution = fzero(#(x) test(x,B,b,a,r,p),5000000)
I haven't tested whether fzero returns sensible results, but this code shouldn't give an error.

Error in MATLAB.Undefined function 'det' for input arguments of type 'embedded.fi'

I have already seen the link Error using fzero in Matlab: Undefined function or method 'det' for input arguments of type 'function_handle'
But I am unable to solye my problem with this link.I am working with fi object in MATLAB. I have one matrix T_1 (2 cross 2) which is conevrted into the fi(T_1,1,32,26,fimath), i.e 32 signed binary number and 26 is the positon of binary point. Now when I try to excute the follwing code
T = mat_G/(mat_sqrt_D)
T_1=fi(T./mat_E,1,32,26,fimath);
multiplier=1/(2*sqrt(det(var_oldS))*abs(det(T_1)));
follwing error appears
Undefined function 'det' for input arguments of type 'embedded.fi'.
So can anyone tell me how can i fix it.
P.S variable var_oldS, mat_G,mat_E, mat_qrt_D has the same fi object properties i.e fi(variable_name,1,32,26,fimath)
If you check the documentation for det, it says that the input must be single or double. Fixed point is probably not supported. As your matrix is of fixed size 4, it's simple to replace the function:
det2=#(M)M(1)*M(4)-M(2)*M(3)
Then use det2 instead of det.

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.

"Not enough input arguments." when using fsolve with nested functions

I am trying to make my own function in matlab to solve for a system of two nonlinear equations, while using a nested function to share some some parameters, here is a sample code:
function y=solve(a,x0)
a;
y=fsolve(nle,x0); % this is line 3
function f=nle(x)
f(1)=x(1)-a*x(1)^2-x(1)*x(2); % this is line 6
f(2)=2*x(2)-x(2)+3*x(1)*x(2);
end
end
Here a is the parameter I want to pass from command line to the function, and x0 is the start point for the fsolve.
However, when I call the function in malab after specifying a=4 and x0=[1 1]', it gave me the following error:
Error using solve/nle (line 6)
Not enough input arguments.
Error in solve (line 3)
y=fsolve(nle,x0);
I'm quite a newbie for matlab, can anybody tell me where I am doing wrong?
Thanks in advance.
EDIT:
I tried substituting the nle with a function handle #nle, but seems something else went wrong:
Undefined function 'fsolve' for input arguments of type 'function_handle'.
Error in solve (line 3)
y=fsolve(#nle,x0);
Doesn't seem to make sense since I checked the documentation for fsolve, and it says it should indeed use a function handle there...
You miss the '#' in front of nle, i.e.
y = fsolve(#nle,x0);
should work.