How can I define f(x) = x/x in MATLAB (Symbolic Computation) - matlab

I try with these commands
x = sym('x');
f(x) = sym('f(x)');
f(x) = x/x;
and
f(x) = sym('x/x');
, but both of them produce
f(x) = 1
(yes.. for every real x including 0)
The question is how I can avoid the pre-evaluation in the command "sym",
or there exists another way to handle this problem.
Thank you very much!
update 21.05.2014:
Let me describe the problem a little bit.
Consider
f(x) = x/x
and
g(x) = 1
It is obvious that domains of f and g are R-{0} and R respectively.
The automatic simplification in sym/syms may lead to lose some info.

The answer from #pabaldonedo is good. It seems that the designers of MuPad and the Symbolic Toolbox made a choice as x/x is indeterminate.
If you actually want 0, Inf, -Inf, or NaN, to result in NaN rather than 1 then you can use symbolic variables in conjunction with an anonymous function:
f = #(x)sym(x)./sym(x);
f([-Inf -1 0 1 Inf NaN])
which returns
ans =
[ NaN, 1, NaN, 1, NaN, NaN]
Or, if the input is already symbolic you can just use this:
f = #(x)x./x;
f(sym([-Inf -1 0 1 Inf NaN]))

There is no pre-evaluation in your code. F(x) = x/x is always 1, even for x = 0, Matlab is just simplifying how the function is expressed but there is no pe-evaluation.
I think you should have a look to indeterminate forms to understand why for x = 0, x/x = 1. Have a look to wikipedia: http://en.wikipedia.org/wiki/Indeterminate_form

Related

matlab: matrix symbolic solution

Thanks much for your time and for all your help.
Actually, I made a mistake in the previous post when specifying the problem. Thus, I reformulate my question using a simpler example. I need to solve symbolically the equation Ct = Z/(P-I) or Ct*(P-I) = Z.
I already know the answer => Ct = [sigma, 1-sigma]
How to program "correctly" the code in order to get the solution
syms sigma;
Ct = sym('Ct',[1 2]);
%
P = [sigma 1-sigma;
sigma 1-sigma];
I = [1 0;
0 1];
Z = [0 0];
%
solve(Ct*(P-I) == Z);
So far, I get :
Z =
0 0
Warning: The solutions are parametrized by the symbols:
z = C_
In solve at 190
In test_matrix_sigma at 13
Or with
solve(Ct == Z/(P-I), Ct);
I get:
Warning: System is rank deficient. Solution is not unique.
Warning: 4 equations in 2 variables.
In /opt/MATLAB/R2013a/toolbox/symbolic/symbolic/symengine.p>symengine at 56
In mupadengine.mupadengine>mupadengine.evalin at 97
In mupadengine.mupadengine>mupadengine.feval at 150
In solve at 170
In test_matrix_sigma at 13
---------------------------------------------------------------------------------------
Thanks for the answer !
Now I have two issues:
1) When I try to handla a more complicated system:
syms a b P1 P2;
I = [1 0 0 0;
0 1 0 0;
0 0 1 0;
0 0 0 1];
%
P = [a*P1 (1-a)*P1 (1-b)*(1-P1) b*(1-P1);
a*P1 (1-a)*P1 (1-b)*(1-P1) b*(1-P1);
b*(1-P2) (1-b)*(1-P2) (1-b)*P2 b*(1-P2);
b*(1-P2) (1-b)*(1-P2) (1-b)*P2 b*(1-P2)];
%
assume(a, 'real');
assume(b, 'real');
assume(P1, 'real');
assume(P2, 'real');
%
answer = null((P-I)');
disp(answer);
I get
ans =
[ empty sym ]
as the only answer.
2) If there is a way in maltlab to "solve" the above symbolic matrix P and find the symbolic determinant ?
For instance, if I do eid(P) it works;
when I do det(P) it gives 0 as answer...
This post is an answer to a different problem, that was first asked by the OP before being edited. I leave the problem and solution here in case someone ever runs into the same problem:
I need to solve symbolically the following matrix equation to find out Ct (a vector ???):
syms a b P1 P2
%
P = [a*P1 (1-a)*P1 (1-b)*(1-P1) b*(1-P1);
a*P1 (1-a)*P1 (1-b)*(1-P1) b*(1-P1);
b*(1-P2) (1-b)*(1-P2) (1-b)*P2 b*(1-P2);
b*(1-P2) (1-b)*(1-P2) (1-b)*P2 b*(1-P2)];
%
solve(Ct*(P-1) == 0, Ct);
How to proceed ?
So far I get:
Undefined function or variable 'Ct'.
Error in matrix_test (line 10) solve(Ct*(P-1) == 0, Ct);
The error you get is because you did not assign Ct before trying to solve for your equation. In the equation Ct*(P-1) == 0, Matlab does not know what Ct is. You could remedy this by creating a symbolic vector (see sym documentation). For instance:
Ct = sym('Ct', [1 4]);
However, using solve on this would not give you the solutions you're looking for: instead, Matlab is going to give you the trivial answer Ct = 0, which of course is a correct answer to your equation.
What you really want to find is the null space of the (P-1)' matrix: the null space is the set of vectors X such that (P-1)'X = 0 (Which is the same thing as X'(P-1) = 0, so Ct = X'). The Matlab function null (see doc) is what you need. Using your code, I get:
null((P-1)')
ans =
[ -1, 0]
[ 1, 0]
[ 0, -1]
[ 0, 1]
This means that any linear combination of the vectors [-1, 1, 0, 0] and [0, 0, -1, 1] belong to the null space of (P-1)', and therefore its transpose is the Ct you were looking for.
N.B.: This result is easily confirmed by observation of your initial matrix P.
This edited problem is only slightly different from the first one. Once again, you are looking to solve an homogeneous system of linear equations.
The warnings you get simply warn you of that: there are an infinity of solutions ; The first warning tells you that the answer is parameterized by C_ (meaning it's in the complex plane, you could add assume(sigma, 'real') and assume(Ct, 'real') if you wanted, you'd get an answer parameterized by R_.
The solution is to find the null space of the matrix (P-I)', as for the previous problem.
null((P-I)')
ans =
-sigma/(sigma - 1)
1
Now if your vector Z became different from 0, you would need to add the particular solution, that is Z/(P-I). In the present case, it gives:
Z/(P-I)
Warning: System is rank deficient. Solution is not unique.
ans =
[ 0, 0]
This means that in this case the particular solution is [0 0], and the result of null gives you the homogeneous solution. Remember that the complete solution of a linear system of equations is the sum of the particular solution + a linear combination of the elements of the null space. A way to express this in Matlab could be:
syms lambda real;
sol = Z/(P-I) + lambda * null((P-I)')'
sol =
[ -(lambda*sigma)/(sigma - 1), lambda]

Summation of sines with limits and a variable?

I need help to add a function, from 1 to 10, using MATLAB. The function is ((1/n)*sin(n*pi*x)) where n goes from 1 to 10 and x stays as a variable. Ultimately I want to have a summation of ten sines (i.e K1*sin(pi*x)+K2*sin(2*pi*x)+k3*sin(3*pi*x)+...etc) where k is a constant. I would really appreciate any assistance. Thanks
Edit: Thanks to everyone who helped with my problem However I should have been more specific when asking my question. After getting the summation, I want to plot the sine series. I tried doing this but I kept getting an error saying that "conversion from sym to double is not possible" Now I tried doing a for loop to get my graph. My code is as follows:
n = 0:10;
while i <= n
for i = 1:length(n);
T = (1/n(i))*sin(n(i)*pi*x);
end
i = 1+i;
max = sum(T);
end
plot(x,max,'black')
However this doesn't work. I don't think that this is the proper way to get the sum of a double. I would really appreciate it if someone could help me again. Thanks again
Learn to exploit MATLAB's vector nature.
For one-off shots:
>> f = #(n,x) sin((1:n)*pi*x) * (1./(1:n).');
>> f(200, 0.5)
ans =
7.828982258896381e-001
To be able to evaluate f(n,x) with vector/matrix input x:
>> f = #(n,x) reshape( sin( bsxfun(#times, (1:n)*pi, x(:)) ) * (1./(1:n).'), size(x) );
>> f(15,rand(2))
ans =
5.077194963950054e-001 2.660834723822258e-001
1.416130930552744e+000 1.012255979042172e-001
Replace (1./(1:n).') by [K1 K2 K3 ...].' when you want to use other constants than 1/n.
From my understanding you are trying to sum the multivariable expression. In your case, you have two variables n and x. You want to sum the expression from n = 1 to 10 keeping x as a variable. You can do this in MATLAB by using symsum function, whose syntax is
symsum(expr,var,a,b)
Where expression expr defines the terms of a series, with respect to the symbolic variable var. The value of the variable var changes from a to b. If you do not specify the variable, symsum uses the default variable determined by symvar. If expr is a constant, then the default variable is x.
So in your case
expr = (1/n*sin(n*pi*x)
var = n
a = 1
b = 10
The simple code would be
>>syms n x
>>F = symsum ((1/sym('n'))*sin(sym('n')*pi*x), n, 1, 10)
Answer to Edit: MATLAB cannot convert the sys variable to double. You can instead substitute the value of sym variable with variable from MATLAB workspace. For example you can plot the above function for the range 0 to 10 by using following command.
>> x = 0:0.1:10;
>> plot(x, subs(F))

NaN when using constrained minimization in Matlab?

I am trying to maximize a function of 10 variables. I do this by the following piece of code:
b = [1 2 3 4 1 7 -1 -4 9 1].'; %// initial value of the decision variable
goal = #(b) -sum(F(b));
Because I want to maximize F for a given set of parameters b, of which the last value should be ≥ 0. I implemented the optimization as follows:
%// lower bounds for decision variable
lb = [-inf(1,9) 0];
%// upper bounds for decision variable
ub = inf(1,10);
myfminoptions = optimset('Display','Iter', 'Algorithm','active-set');
theta1 = fmincon(goal, theta0, [],[],[],[], lb,ub, [], myfminoptions);
That is, I find the minimum of the negative of the function, which should be the same as finding the maximum of that function.
My problem is, that for every iteration I get that the first-order optimality is Inf and thus 'Hessian not updated'.
Additionally the value of my function NaN in the first iteration, which I simply don't understand. This might be the cause, but I not sure.
Edit
This is the function that I am using:
function ll = mytobit(theta);
global x y;
b=theta(1:size(theta,1)-1);
s=theta(size(theta,1));
ll = (y==0).*log(1-normcdf(x*b/s))+ (y>0).*(-0.5*(log(2*pi)+log(s^2)+(y-x*b).^2/s^2));
return;
When calling the function for the theta0, I get N likelihood values (N is the number of rows in X and Y) which I am supposed to.
An example of Y and X:
Y = [0;0;2047;1890;1975]
X = [2300, 34, 1156, 0, 1;
2100, 35, 1225, 0, 1;
2760, 36, 1296, 1, 0;
2300, 37, 1369, 1, 0
2455, 38, 1444, 0, 0]
You should probably be using the 'sqp' algorithm, as opposed to active-set. The sqp algorithm can recover from NaNs produced when s strays to close to zero, where the derivatives are undefined.
Also, you would hopefully not be initializing the s variable near zero in theta0.
Because s is actually supposed to be strictly greater than zero, it may help to re-express it as s=exp(-d/2) where d is a new unconstrained unknown variable. Also, your objective function only depends on b through the quantity B=b/s. So, you could formulate this way
ll = (y==0).*log(1-normcdf(x*B))+ (y>0).*(-0.5*(log(2*pi)-d+(y*exp(-d/2)-x*B).^2));
Now there are no ill-defined regions in the function or its derivatives.

MATLAB: Test if anonymous vector is a subset of R^n

I'm trying to use MatLab code as a way to learn math as a programmer.
So reading I'm this post about subspaces and trying to build some simple matlab functions that do it for me.
Here is how far I got:
function performSubspaceTest(subset, numArgs)
% Just a quick and dirty function to perform subspace test on a vector(subset)
%
% INPUT
% subset is the anonymous function that defines the vector
% numArgs is the the number of argument that subset takes
% Author: Lasse Nørfeldt (Norfeldt)
% Date: 2012-05-30
% License: http://creativecommons.org/licenses/by-sa/3.0/
if numArgs == 1
subspaceTest = #(subset) single(rref(subset(rand)+subset(rand))) ...
== single(rref(rand*subset(rand)));
elseif numArgs == 2
subspaceTest = #(subset) single(rref(subset(rand,rand)+subset(rand,rand))) ...
== single(rref(rand*subset(rand,rand)));
end
% rand just gives a random number. Converting to single avoids round off
% errors.
% Know that the code can crash if numArgs isn't given or bigger than 2.
outcome = subspaceTest(subset);
if outcome == true
display(['subset IS a subspace of R^' num2str(size(outcome,2))])
else
display(['subset is NOT a subspace of R^' num2str(size(outcome,2))])
end
And these are the subset that I'm testing
%% Checking for subspaces
V = #(x) [x, 3*x]
performSubspaceTest(V, 1)
A = #(x) [x, 3*x+1]
performSubspaceTest(A, 1)
B = #(x) [x, x^2, x^3]
performSubspaceTest(B, 1)
C = #(x1, x3) [x1, 0, x3, -5*x1]
performSubspaceTest(C, 2)
running the code gives me this
V =
#(x)[x,3*x]
subset IS a subspace of R^2
A =
#(x)[x,3*x+1]
subset is NOT a subspace of R^2
B =
#(x)[x,x^2,x^3]
subset is NOT a subspace of R^3
C =
#(x1,x3)[x1,0,x3,-5*x1]
subset is NOT a subspace of R^4
The C is not working (only works if it only accepts one arg).
I know that my solution for numArgs is not optimal - but it was what I could come up with at the current moment..
Are there any way to optimize this code so C will work properly and perhaps avoid the elseif statments for more than 2 args..?
PS: I couldn't seem to find a build-in matlab function that does the hole thing for me..
Here's one approach. It tests if a given function represents a linear subspace or not. Technically it is only a probabilistic test, but the chance of it failing is vanishingly small.
First, we define a nice abstraction. This higher order function takes a function as its first argument, and applies the function to every row of the matrix x. This allows us to test many arguments to func at the same time.
function y = apply(func,x)
for k = 1:size(x,1)
y(k,:) = func(x(k,:));
end
Now we write the core function. Here func is a function of one argument (presumed to be a vector in R^m) which returns a vector in R^n. We apply func to 100 randomly selected vectors in R^m to get an output matrix. If func represents a linear subspace, then the rank of the output will be less than or equal to m.
function result = isSubspace(func,m)
inputs = rand(100,m);
outputs = apply(func,inputs);
result = rank(outputs) <= m;
Here it is in action. Note that the functions take only a single argument - where you wrote c(x1,x2)=[x1,0,x2] I write c(x) = [x(1),0,x(2)], which is slightly more verbose, but has the advantage that we don't have to mess around with if statements to decide how many arguments our function has - and this works for functions that take input in R^m for any m, not just 1 or 2.
>> v = #(x) [x,3*x]
>> isSubspace(v,1)
ans =
1
>> a = #(x) [x(1),3*x(1)+1]
>> isSubspace(a,1)
ans =
0
>> c = #(x) [x(1),0,x(2),-5*x(1)]
>> isSubspace(c,2)
ans =
1
The solution of not being optimal barely scratches the surface of the problem.
I think you're doing too much at once: rref should not be used and is complicating everything. especially for numArgs greater then 1.
Think it through: [1 0 3 -5] and [3 0 3 -5] are both members of C, but their sum [4 0 6 -10] (which belongs in C) is not linear product of the multiplication of one of the previous vectors (e.g. [2 0 6 -10] ). So all the rref in the world can't fix your problem.
So what can you do instead?
you need to check if
(randn*subset(randn,randn)+randn*subset(randn,randn)))
is a member of C, which, unless I'm mistaken is a difficult problem: Conceptually you need to iterate through every element of the vector and make sure it matches the predetermined condition. Alternatively, you can try to find a set such that C(x1,x2) gives you the right answer. In this case, you can use fminsearch to solve this problem numerically and verify the returned value is within a defined tolerance:
[s,error] = fminsearch(#(x) norm(C(x(1),x(2)) - [2 0 6 -10]),[1 1])
s =
1.999996976386119 6.000035034493023
error =
3.827680714104862e-05
Edit: you need to make sure you can use negative numbers in your multiplication, so don't use rand, but use something else. I changed it to randn.

how to eliminate complex number in a vector in Matlab

In Matlab, suppose there is a vector whose elements can be complex or real. I was wondering how to remove the nonreal elements, and consequently reduce the size of the vector? Thanks and regards!
Use the REAL and IMAG functions:
>> x = [1+i; 4+3i; 5+6i]
x =
1 + 1i
4 + 3i
5 + 6i
>> real(x)
ans =
1
4
5
>> imag(x)
ans =
1
3
6
EDIT
The above doesn't answer the poster's question. This does.
Use the FIND and REAL functions:
>> v = [1+i; 2; 3]
v =
1 + 1i
2
3
>> v(v == real(v))
ans =
2
3
You can also avoid testing in a loop using Matlab's vector syntax:
x = [1, 2, 3+1i, 4+2i, 5+3i, 6, 7, 8+4i, 9+0.000001i]
y = x(imag(x) == 0);
z = real(x(abs(imag(x)) < 0.00001));
y should be [1,2,6,7] and z should be [1,2,6,7,9]
imag(x) == 0 yields a logical vector with true values whenever the equality condition holds. x(<logical vector>) yields a new vector with only the elements of x where <logical vector> was true.
That's a very unusual thing to ask. Since the imaginary part is a floating point number, you can't really tell which number is a real number and which number is very close to a real number. Such function doesn't exist in Matlab since it's not very clear how it would be useful for anything (it doesn't make much sense to remove those numbers). Specifying your actual purpose here might help you get better answers.
If you want to ensure that only real numbers are left in the vector, you can use the following (it doesn't work with matrices and vertical rows, but you've got the idea):
x = [1, 2, 3+1i, 4+2i, 5+3i, 6, 7, 8+4i, 9+0i]
z = []
for k = [1:length(x)]
if imag(x(k)) == 0
z = [z, real(x(k))]
endif
endfor
If you want to keep all numbers that are close to a real number, but could have some small non-zero imaginary part, you can use the following:
x = [1, 2, 3+1i, 4+2i, 5+3i, 6, 7, 8+4i, 9+0.000001i]
z = []
for k = [1:length(x)]
if abs(imag(x(k))) < 0.00001
z = [z, real(x(k))]
endif
endfor
Of course, if you tell us what your actual criterion is, it would be much easier to give you a better idea. Are you looking for the real solutions to some sort of equation or system of equations, real roots of a polynomial? In this case, the first one might miss a real solution because of the approximation error, and the second one can give you things that aren't solutions.