Using unspecified constants in matlab - matlab

I'm trying to solve a system of equations in the s-domain. So set up this system of equations in matrix form:
a=[.4*s+s+5 -5; -5 .5*s+5]
c=[3/s; 3/(2*s)]
(1/s)*a*b=c
I just get the error that s is undefined.
How can I solve for b in terms of s?

Matlab does not (naturally) do symbolic calculations --- which is what your code is trying to do. Matlab's variables need to be concrete numbers, or arrays, or structures, etc. They cannot just be placeholders for arbitrary numbers.
(UNLESS: You use the symbolic computing toolbox for Matlab. I haven't really used this because I prefer to do symbolic computing in environments such as Maple or Mathematica. You could even solve your problem on the Wolfram Alpha website)
But if you pick a specific value of s, computing what you want is easy:
s = 5;
a=[.4*s+s+5 -5; -5 .5*s+5];
c=[3/s; 3/(2*s)];
b = s*(a\c);
Where I have used the backslash operator for doing linear inversion.
You should now have that
(1/s)*a*b-c
is the zero vector.
EDIT: I looked into the symbolic toolbox. It looks like this is what you want (but you need to have the symbolic toolbox licensed and installed for it to work):
syms s;
a=[.4*s+s+5 -5; -5 .5*s+5];
c=[3/s; 3/(2*s)];
b = simple(s*(a\c))

The code to perform your calculation using symbolic operators is:
syms s; %This defines 's' as a symbolic token
a=[.4*s+s+5 -5; -5 .5*s+5]; %a and c inherit the symbolic properties from s
c=[3/s; 3/(2*s)];
result = solve('(1/s)*a*b=c','b') %Solve is the general symbolic toolbox algebraic solver.
This produces
result =
(c*s)/a
Generally speaking, Matlab performs best as a numerical toolbox. So depending on your application I would go with another approach, such as that demonstrated by Ian Hincks in another answer. But sometimes the situation demands a symbolic solution.

Related

Matlab equivalent to Mathematica's FindInstance

I do just about everything in Matlab but I have yet to work out a good way to replicate Mathematica's FindInstance function in Matlab. As an example, with Mathematica, I can enter:
FindInstance[x + y == 1 && x > 0 && y > 0, {x, y}]
And it will give me:
{{x -> 1/2, y -> 1/2}}
When no solution exists, it will give me an empty Out. I use this often in my work to check whether or not a solution to a system of inequalities exists -- I don't really care about a particular solution.
It seems like there should be a way to replicate this in Matlab with Solve. There are sections in the help file on solving a set of inequalities for a parametrized solution with conditions. There's another section on spitting out just one solution using PrincipalValue, but this seems to just select from a finite solution set, rather than coming up with one that meets the parameters.
Can anybody come up with a way to replicate the FindInstance functionality in Matlab?
Building on what jlandercy said, you can certainly use MATLAB's linprog function, which is MATLAB's linear programming solver. A linear program in the MATLAB universe can be formulated like so:
You seek to find a solution x in R^n which minimizes the objective function f^{T}*x subject to a set of inequality constraints, equality constraints, and each component in x is bounded between a lower and upper bound. Because you want to find the minimum possible value that satisfies the above constraint given, what you're really after is:
Because MATLAB only supports inequalities of less than, you'll need to take the negative of the first two constraints. In addition, MATLAB doesn't support strict inequalities, and so what you'll have to do is enforce a constraint so that you are checking to see if each variable is lesser than a small number, perhaps something like setting a threshold epsilon to 1e-4. Therefore, with the above, your formulation is now:
Note that we don't have any upper or lower bounds as those conditions are already satisfied in the equality and inequality constraints. All you have to do now is plug this problem into linprog. linprog accepts syntax in the following way:
x = linprog(f,A,b,Aeq,beq);
f is a vector of coefficients that work with the objective function, A is a matrix of coefficients that work with the inequality, b is a vector of coefficients that are for the right-hand side of each inequality constraint, and Aeq,beq, are the same as the inequality but for the equality constraints. x would be the solution to the linear programming problem formulated. If we reformulate your problem into matrix form for the above, we now get:
With respect to the linear programming formulation, we can now see what each variable in the MATLAB universe needs to be. Therefore, in MATLAB syntax, each variable becomes:
f = [1; 1];
A = [-1 0; 0 -1];
b = [1e-4; 1e-4];
Aeq = [1 1];
beq = 1;
As such:
x = linprog(f, A, b, Aeq, beq);
We get:
Optimization terminated.
x =
0.5000
0.5000
If linear programming is not what you're looking for, consider looking at MATLAB's MuPAD interface: http://www.mathworks.com/help/symbolic/mupad_ug/solve-algebraic-equations-and-inequalities.html - This more or less mimics what you see in Mathematica if you're more comfortable with that.
Good luck!
Matlab is not a symbolic solver as Mathematica is, so you will not get exact solutions but numeric approximations. Anyway if you are about to solve linear programming (simplex) such as in your example, you should use linprog function.

Solve a polynomial equation of degree 4

I got this equation after I solved that problem here
I would like to solve it using Matlab.
The function roots returns polynomial roots. Simply pass the coefficients:
roots([1,-6,-36,216,-324])
Use solve from MATLAB's symbolic mathematics toolbox:
>> syms x;
>> y = solve('x^4-6*x^3-36*x^2+216*x-324==0')
y =
7.7446378738164683022795580182987
-6.3360292312480789716536487435108
2.2956956787158053346870453626061 - 1.1543655214730370697054534567177*i
2.2956956787158053346870453626061 + 1.1543655214730370697054534567177*i
The first line of code declares that x is a symbolic mathematics variable that we can use with the toolbox. Next, we use solve and we put in a string that describes the equation that is seen in your post. Bear in mind that when we multiply coefficients, you need to use the * operator, and for equality, we need to use double equals, or ==. The output should give you four roots, as dictated by the fundamental theorem of algebra. You'll see that you have two real roots, as well as two imaginary roots.
Alternatively, you can use roots in MATLAB by specifying a vector of coefficients starting from the highest order down to the lowest as per Daniel's answer.

How to solve an equation with piecewise defined function in Matlab?

I have been working on solving some equation in a more complicated context. However, I want to illustrate my question through the following simple example.
Consider the following two functions:
function y=f1(x)
y=1-x;
end
function y=f2(x)
if x<0
y=0;
else
y=x;
end
end
I want to solve the following equation: f1(x)=f2(x). The code I used is:
syms x;
x=solve(f1(x)-f2(x));
And I got the following error:
??? Error using ==> sym.sym>notimplemented at 2621
Function 'lt' is not implemented for MuPAD symbolic objects.
Error in ==> sym.sym>sym.lt at 812
notimplemented('lt');
Error in ==> f2 at 3
if x<0
I know the error is because x is a symbolic variable and therefore I could not compare x with 0 in the piecewise function f2(x).
Is there a way to fix this and solve the equation?
First, make sure symbolic math is even the appropriate solution method for your problem. In many cases it isn't. Look at fzero and fsolve amongst many others. A symbolic method is only needed if, for example, you want a formula or if you need to ensure precision.
In such an old version of Matlab, you may want to break up your piecewise function into separate continuous functions and solve them separately:
syms x;
s1 = solve(1-x^2,x) % For x >= 0
s2 = solve(1-x,x) % For x < 0
Then you can either manually examine or numerically compare the outputs to determine if any or all of the solutions are valid for the chosen regime – something like this:
s = [s1(double(s1) >= 0);s2(double(s2) < 0)]
You can also take advantage of the heaviside function, which is available in much older versions.
syms x;
f1 = 1-x;
f2 = x*heaviside(x);
s = solve(f1-f2,x)
Yes, the Heaviside function is 0.5 at zero – this gives it the appropriate mathematical properties. You can shift it to compare values other than zero. This is a standard technique.
In Matlab R2012a+, you can take advantage of assumptions in addition to the normal relational operators. To add to #AlexB's comment, you should convert the output of any logical comparison to symbolic before using isAlways:
isAlways(sym(x<0))
In your case, x is obviously not "always" on one side or the other of zero, but you may still find this useful in other cases.
If you want to get deep into Matlab's symbolic math, you can create piecewise functions using MuPAD, which are accessible from Matlab – e.g., see my example here.

How to get Gamma function in Mathematica using Matlab

In Mathematica, Gamma[a, z] refers to the upper incomplete Gamma function whereas in Matlab, gammainc(z, a) refers to the regularized lower incomplete Gamma function. I want to know how I can obtain the Mathematica result using Matlab? In this link, the method for obtaining same Matlab result using Mathematica was explained, but I couldn't find a strategy of getting one via Matlab.
You have several options. In addition to the version offered by #MarkMcClure
y = (1-gammainc(z,a)).*gamma(a)
you can also use additional arguments to get the upper regularized gamma function directly:
y = gammainc(z,a,'upper').*gamma(a)
Note that the order of the arguments is opposite to that of Mathematica's function.
The above are strictly numeric functions, but Mathematica's Gamma evaluates symbolically. You can use Matlab's igamma in the symbolic toolbox. Note that this function is not regularized and used the same argument order as Mathematica's function – it's as close to a direct equivalent as you'll find (but obviously slower for numeric evaluation):
syms a z;
y = igamma(a,z)
In older versions of Matlab, this function may not be directly available. You can however, still access the MuPAD version via:
y = feval(symengine,'igamma',a,z)
or something like
y = evalin(symengine,['igamma(' char(a) ',' char(z) ')'])
According to Wikipedia,
Thus, in Mathematica you might have
Gamma[1, 2] // N
While in Matlab you could have
gamma(1)-gammainc(2,1)
Both return 0.135335

Pi, Matlab Symbolic math toolbox have a bug?

Hello i have one question. When I calculate a division in matlab: x/(pi.^2)
syms x
x/(pi.^2)
ans =
(281474976710656*v)/2778046668940015
the correct answer is x/9.8696, so why is matlab giving me this result?
Is it a bug?
You have to use the vpa() command "Variable-precision arithmetic". Check this code:
syms x real; % define x as a real symbolic variable (not a complex variable)
vpa( x/(pi.^2), 5) % second argument define number of significant digits
For trigonometric expressions involving pi, it is sometimes good to define sym('pi'):
syms x real;
pi_s = sym('pi');
expr = x/pi_s^2
I try to always use the 'real' tag when using the symbolic toolbox. If you do not use it you are going to see a lot of complex conjugates and other things that are not important for your problem, because x is probably real variable.
Hope this helps,
No it is not a bug:
2778046668940015/281474976710656 = 9.8696