Matlab: Polynomial Expansion Routine - matlab

In Mathematica, it's easy to expand terms like
(ax^2+bx+c)^n
But is there anyway I can do this in Matlab?

For any arbitrary expression: not without the Symbolic Toolbox.
http://www.mathworks.com/help/toolbox/symbolic/expand.html
However, if you wish to expand polynomials, you can use the conv function. Just run it in a loop.
a = 1;
b = 2;
c = 3;
n = 5;
soln = [a b c];
for i=1:n-1
soln = conv(soln,[a b c]);
end

You can also use my sympoly toolbox.
>> sympoly a b c x
>> (a*x^2+b*x+c)^3
ans =
c^3 + 3*b*c^2*x + 3*b^2*c*x^2 + b^3*x^3 + 3*a*c^2*x^2 + 6*a*b*c*x^3 + 3*a*b^2*x^4 + 3*a^2*c*x^4 + 3*a^2*b*x^5 + a^3*x^6

Related

Finding Cubic Polynomials

I have an equation: y=ax^3 + bx^2 + cx + d and the list of values x = 1, 2, 3, 4 when y = 3, 4, 3, -6 respectively. In Octave, I want to:
(a) Set up a system of four equations involving a, b, c and d. For example, substituting (x, y) = (1,3) into the polynomial gives the equation 3 = a + b + c + d.
(b) Solve the system in (a).
I've been trying to find how to do this for three hours and found nothing. Any help would be appreciated
Thanks.
pstscrpt - I have to do everything in Octave, even though I could find it by hand
Written without any ; at end of assignements so you can see what is going on.
You problem is basically a linear system in the variables [a,b,c,d]'=z
So you need to build a system A*z=y, where A is a matrix 4x4, y and z are column vector size 4
x=[1,2,3,4]'
y=[3,4,3,-6]'
A=zeros(4,4)
for i=1:4
A(i,:)= [ x(i)^3, x(i)^2, x(i), 1]
endfor
z=A\y
the outcome will be
z =
-1.00000
5.00000
-7.00000
6.00000
In Matlab: start by just substituting the different values of x and y you wrote in the expression a*x^3 + b*x^2 + c*x + d = y as:
syms a b c d
eqn1 = a*1^3 + b*1^2 + c*1^1 +d == 3 ;
eqn2 = a*2^3 + b*2^2 + c*2^1 +d == 4 ;
eqn3 = a*3^3 + b*3^2 + c*3^1 +d == 3 ;
eqn4 = a*4^3 + b*4^2 + c*4^1 +d == -6 ;
Then Use equationsToMatrix to convert the equations into the form AX = B. The second input to equationsToMatrix specifies the independent variables in the equations.:
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4], [a, b, c,d ])
and the solution for a,b,c,d is:
X = linsolve(A,B)
you can also use if you want
sol = solve([eqn1, eqn2, eqn3, eqn4], [a, b, c,d ])

How to simplify functions in matlab?

Hello Let's say I have theres two functions
F1= a*x^(2) + b
F2 = c*x
Where a, b and c are a constant and x is a variablem how do can I make matlab gives me a simplified version of F1*F2 so the answer may be
a*c*x^(3) + b*c*x
This is what I have in matlab
syms x a b c
F1 = a*x^(2) +b;
F2 = c*x^(2);
simplify(F1*F2)
ans =
c*x^2*(a*x^2 + b)
When I multiply in matlab it's just giving me (ax^(2) + b)(c*x)
Try this commands:
syms a x b c
F1= a*x^(2) + b
F2 = c*x
F=F1*F2
collect(F)
which will give you:
ans =
a*c*x^3 + b*c*x
The command collect is useful when working with polynoms. The opposite command is pretty. It will give you c*x*(a*x^2 + b)

fminsearch error: DOUBLE cannot convert the input expression into a double array

I am encountering a problem during an optimization exercise. I am trying to use fminsearch() in matlab to solve it. The following error is generated when the code is run:
The following error occurred converting from sym to double: Error
using symengine (line 59) DOUBLE cannot convert the input expression
into a double array. If the input expression contains a symbolic
variable, use VPA.
Error in fminsearch (line 190) fv(:,1) = funfcn(x,varargin{:});
Error in Optimization (line 22) sol2 = fminsearch(J, x0);
The script I use can be seen below. The f is the minimization problem where g1 & g2 are constraints. The p is there so that I can turn it into for loop later.
syms x1 x2 x3 x4;
syms p;
f = x1.^4 - x3.^2 + x1*x3 + x2*x4 - 5*x2 + 3;
g1 = x1.^2 + x2.^2 + x3.^2 + x4.^2 - 1;
g2 = x1 + x3 - 1;
x0 = [2 2 2 2];
p = 3;
J = #(x1,x2,x3,x4) sym(f + p * g1.^2 + g2.^2);
sol2 = fminsearch(J, x0);
This Stackoverflowpost has the same problem but in another perspective.According to this post it might be a problem with allocating the in a valid way. I tried a few different ways to solve my problem. I have tried matlabFunction() and putting the function in a seperate file.
If the input expression contains a symbolic variable, use the VPA function instead?
Thanks in advance for the help.
fminsearch is designed for numerical minimization. There is little point in using symbolic math in this case (it can be used, but it will be slower, complicate your code, and the results will still be in double precison). Second, if you read the documentation and look at the examples for fminsearch, you'll see that it requires a function that takes a single vector input (as opposed to four scalars in your case). Here's how you could rewrite your equations using anonymous functions:
f = #(x)x(1).^4 - x(3).^2 + x(1).*x(3) + x(2).*x(4) - 5*x(2) + 3;
g1 = #(x)x(1).^2 + x(2).^2 + x(3).^2 + x(4).^2 - 1;
g2 = #(x)x(1) + x(3) - 1;
x0 = [2 2 2 2];
p = 3;
J = #(x)f(x) + p*g1(x).^2 + g2(x).^2;
sol2 = fminsearch(J, x0)
this returns
sol2 =
0.149070165097281 1.101372214292880 0.326920462283209 -0.231885482601008
Using symbolic math and subs:
syms x1 x2 x3 x4;
f = x1^4 - x3^2 + x1*x3 + x2*x4 - 5*x2 + 3;
g1 = x1^2 + x2^2 + x3^2 + x4^2 - 1;
g2 = x1 + x3 - 1;
x0 = [2 2 2 2];
p = sym(3);
J = #(X)subs(f + p*g1^2 + g2^2,[x1 x2 x3 x4],X);
sol2 = fminsearch(J, x0)
which returns identical results.

How can I solve a system of 4 equations and 4 unknowns with MATLAB?

I have a general equation
t=tr+(ts-tr)/(1+(a*h)^n)^(1-1/n)
for (h=0, 1, 2, 3), I have t=2.000, 1.6300, 1.2311, 1.1084. therefor there are 4 equations with 4 unknowns tr, ts, a, n
I used "solve" function in matlab
s=solve('tr+(ts-tr)/(1+(a*0)^n)^(1-1/n)=2','tr+(ts-tr)/(1+(a*1)^n)^(1-1/n)=1.63','tr+(ts-tr)/(1+(a*2)^n)^(1-1/n)=1.2311','tr+(ts-tr)/(1+(a*3)^n)^(1-1/n)=1.1084')
and error is
??? Error using ==> mupadmex
Error in MuPAD command: Singularity [ln];
during evaluation of 'numeric::fsolve'
Error in ==> sym.sym>sym.mupadmexnout at 2018
out = mupadmex(fcn,args{:});
Error in ==> solve at 76
[symvars,R] = mupadmexnout('symobj::solvefull',eqns,vars);
What should I do?
The problem appears with you using the solve function. That only works for simple equations, it is better to use the fsolve function. Due to the fact that I am worried that I am doing an assignment for you, I am only going to show you how to do another example using fsolve.
Suppose that you want to solve
1 = x_1
1 = x_1 + x_2
-1 = x_1 + x_2 + x_3
-1 = x_1 + x_2 + x_3 + x_4
then what you firstly need to do is write these as equations equal 0
0 = x_1 - 1
0 = x_1 + x_2 - 1
0 = x_1 + x_2 + x_3 + 1
0 = x_1 + x_2 + x_3 + x_4 + 1
then you need to write a function that takes in a vector x, the components of x will represent x_1, x_2, x_3 and x_4. The output of the function will also be a vector whose components should the outputs of the Right hand side of the above equations (see the function fun below). This function is going to be called by fSolve for it to provide it with guesses of the correct value of x, until it guess correct. When never actually run this function ourselves. That is why it is below the top function.
Then you create a function handle to this function by fHandle = #fun. You can think of fHandle as another name for fun, when we calculate fHandle([1; 2; 3; 4]) this is the same as calculating fun([1; 2; 3; 4]). After this you make an initial guess of the correct vector x, say we chose xGuess = [1; 1; 1; 1]. Finally we pass fHandle and xGuess to fSolve.
Here is the code
function Solve4Eq4Unknown()
fHandle = #fun;
xGuess = ones(4,1);
xSolution = fsolve(fHandle, xGuess)
end
function y = fun(x)
y = zeros(4,1); % This step is not necessary, but it is effecient
y(1) = x(1) - 1;
y(2) = x(1) + x(2) - 1;
y(3) = x(1) + x(2) + x(3) + 1;
y(4) = x(1) + x(2) + x(3) + x(4) + 1;
end

Matlab: Fsolve giving incorrect roots

I'm trying to solve this system:
x = a + e(c - e*x/((x^2+y^2)^(3/2)))
y = b + c(d - e*y/((x^2+y^2)^(3/2)))
I'm using fsolve, but not matter what I put in as the starting points for the iteration, I get the answer that the starting points are the roots of the equation.
close all, clear all, clc
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
fsolve(#u1FsolveFUNC, [1,2])
Function:
function outvar = u1FsolveFUNC(invar)
global a b c d e
outvar = [...
-invar(1) + a + e*(c - e*(invar(1) / ((invar(1)^2 + invar(2)^2)^(3/2)))) ;
-invar(2) + b + e*(d - e*(invar(2) / ((invar(1)^2 + invar(2)^2)^(3/2))))]
end
I could try with [1,2] as invariables, and it will say that that is a root to the equation, alltough the correct answer for [1,2] is [12.76,15.52]
Ideas?
If you write your script like this
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
f = #(XY) [...
-XY(1) + a + e*(c - e*(XY(1) ./ (XY(1).^2 + XY(2).^2).^(3/2)))
-XY(2) + b + e*(d - e*(XY(2) ./ (XY(1).^2 + XY(2).^2).^(3/2)))];
fsolve(f, [1,2])
it is a lot clearer and cleaner. Moreover, it works :)
It works because you haven't declared a,b,c,d and e to be global before you assigned values to them. You then try to import them in your function, but at that time, they are still not defined as being global, so MATLAB thinks you just initialized a bunch of globals, setting their initial values to empty ([]).
And the solution to an empty equation is the initial value (I immediately admit, this is a bit counter-intuitive).
So this equation involves some inverse-square law...Gravity? Electrodynamics?
Note that, depending on the values of a-e, there may be multiple solutions; see this figure:
Solutions are those points [X,Y] where Z is simultaneously zero for both equations. for the values you give, there is a point like that at
[X,Y] = [15.958213798693690 13.978039302961506]
but also at
[X,Y] = [0.553696225634946 0.789264790080377]
(there's possibly even more...)
When you are using global command you have to use the command with all the variables in each function (and main workspace).
eg.
Main script
global a b c d e % Note
a = 1; b = 2; c = 3; d = 4; e = 5;
fsolve(#u1FsolveFUNC,[1,2])
Function
function outvar = u1FsolveFUNC(invar)
global a b c d e % Note
outvar = [-invar(1) + a + e*(c - e*(invar(1) / ((invar(1)^2 + invar(2)^2)^(3/2)))) ; -invar(2) + b + e*(d - e*(invar(2) / ((invar(1)^2 + invar(2)^2)^(3/2))))]