How to take a polynomial form as input in matlab? - matlab

i have a polynomial of the form 𝑓(π‘₯) = π‘Ž0 + π‘Ž1π‘₯ + β‹― +π‘Žπ‘›βˆ’1π‘₯π‘›βˆ’1+π‘Žπ‘›π‘₯𝑛.I want to take a0,a1...an from user and n = length(a0,a1...an) example:
f = 3*x^2 - 4*x + 2.Help me please,thank you.

a0=1;a1=3;a2=4;
A = [a0 a1 a2];
polyval(A,3)
ans =
22
using polyval. Note that the order of input arguments goes from highest to lowest, so in this example the polynomial is x^2+3x+4.
Since MATLAB is a numeric package, you'd generally want the numeric values of the polynomial evaluated at discrete values of x, and not a symbolic expression.

Related

How can I factor specific variables out of a formula in Matlab?

Suppose I have a column vector of formulae like this
N =
4*k2 + 5*k3 + k1*x
7*k2 + 8*k3 + k1*y
and a column vector of symbolic variables like this
k =
k1
k2
k3
The formulae are linear with respect to k. I'd like to find a matrix M such that M*k equals N.
I can do this with N/k. However, that gives
[ (4*k2 + 5*k3 + k1*x)/k1, 0, 0]
[ (7*k2 + 8*k3 + k1*y)/k1, 0, 0]
which is correct, but not what I want. What I want is the matrix
x 4 5
y 7 8
which seems to me the simplest answer in that it involves no variables from k.
How do I convince Matlab to factor out the specified variables from a formula or a vector of formulae?
You can use coeffs, specifically the form
C = coeffs(p,vars) returns coefficients of the multivariate polynomial p with respect to the variables vars.
Since the first input needs to be a polynomial, you need to pass each component of N:
coeffs(N(1), k)
coeffs(N(2), k)
Or use a loop and store all results in a symbolic array:
result = sym('result', [numel(N) numel(k)]); % create symbolic array
for m = 1:numel(N)
result(m,:) = coeffs(N(m), k);
end
In your example, this gives
result =
[ 5, 4, x]
[ 8, 7, y]
Based on #LuisMendo's answer, I used coeffs. But there are a couple of problems with coeffs. The first is that its result doesn't include any coefficients that are 0. The second is that it doesn't seem to guarantee that the coefficients are ordered the same way as the variables in its second argument. I came up with the following function to replace coeffs.
Luckily coeffs returns a second result that lists the variables associated with each item in the first result. (It's more complicated if the formula is not linear.)
function m = factorFormula(f, v )
% Pre: f is a 1x1 sym representing a
% linear function of the variables in v.
% Pre: v is a column vector of variables
% Post: m is a row vector such that m*v equals f
% and the formulas in m do not contain the
% variables in v
[cx,tx] = coeffs(f,v)
n = size(v,1)
m = sym(zeros(1,n))
for i = 1:n
j = find(tx==v(i))
if size(j,2) == 1
m(i) = cx(j)
end
end
end
This only works for one formula, but it can be extended to a vector using the loop in #LuisMendo's answer or this equivalent expression in #Sanchises comment there.
cell2sym(arrayfun( #(f)factorFormula(f,k),N,'UniformOutput',false ) )
I hope there is a better answer than this.

What is f(2) in the following code?

The code (written in Octave) is:
x=1:2:5;
y=1:1:3;
z=1:0.1:1.2;
f=[x+y+z,x.^2+z;sin(x.*y.*z),cos(x)];
h=x(2)-x(1);
xFor=x(1:end-1);
dffor=(f(2:end)-f(1:end-1))/h;
f(2)
dffor
The output I get is
Hello World
ans = 0.84147
dffor = -1.07926 2.62926 -2.89423 4.44423 4.77985 -5.54500 13.59500 -12.95817
I do not understand some of the code. What does f(2) evaluate?
I actually want to get the numerical derivative of the matrix with respect to x. I thought this was the method of forward differences. Also, why am I getting a [1x11] matrix as the output for dffor, which is supposed to be the numerical differentiation matrix?
first, f is a 2D matrix in your code (size [2,6]) and I assume you meant to have a vector (size [1,12]).
dffor is indeed the forward diff. and it has 11 elements (rather than 12 as f) because it has the differences between each consequent pair of f: each element is used twice except for the first and last: (10*2 + 1 + 1)/2 = 11.
f(2) is just the second element of f which equals x(2) + y(2) + z(2)

multipoint boundary value ode with varying coefficients in matlab

I have a second order differential equation given as:
d²u/dz²=a+bu, where a and b are constants that vary in different intervals and z∈[0,Zn]
Intervals are given as I1:z∈[0,Z1), I2:z∈[Z1, Z2), ..., I(n):z∈[Z(n-1), Z(n)] and {a,b} constants varies as {a1,b1} in I1, {a2,b2} in I2,...,{an,bn} in I(n).
Boundary condition is given as u(z=0)=U0, u(z=Zn)=Umax.
A continuous graph of u vs z is required.
I thought of using bvp5c in matlab but bvp5c can solve multipoint boundary value problems where a = a0 < a1 < a2 < ... < an = b in the interval [a,b]. The points a1,a2, ... ,an–1 represent interfaces that divide [a,b] into regions. The problem is my constants(a,b in above ode) is also varying in different intervals.
You can do it. If you read the bvp5c docs, you will see that it takes a function handle (odefun) which takes the z, and u arguments. Simply return the correct value for the RHS based on value of the argument z. Note that you have to convert your 2nd order equation to a 1st order one.
du/dz = v
dv/dz = a(z) + b(z)u
You need to return the vector [v, a(z) + b(z)u] from the two arguments z and [u,v] that your odefun will be called with.

Get an array of results of a function using as input an array of values (in Matlab)

I want to get the array of results of a function using as input an array of values. The function receives two variables (x1, x2) and a constant x3, so I'm trying to input all combination of it in a range using mesh.
The result is incorrect, I'm missing something.
Sample:
fun = #(x1,x2,x3) (x2-x1^2)^2+(1-x1)^2 + x3;
x3 = 7;
fun2 = #(x) fun(x(1,1),x(1,2),x3);
x0 = [2 3];
min = fminsearch(fun2, x0);
disp(min);
x = min(1)-10:1:min(1)+10;
y = min(2)-10:1:min(2)+10;
[X,Y] = meshgrid(x,y);
% I'm getting strange values here, like z < 0, how it is possible if everything is squared in the function.
Z = fun(X,Y,x3);
It's important to note that there is a difference between matrix and element-wise operations in MATLAB.
Matrix operations are defined via plain operators, such as *, or ^. So for example, A*B performs a matrix multiplication between A and B.
Element-wise operators make use of the dot . before the operator, i.e., .*, .^, and so on. Thus, A.*B performs an element-wise multiplication of A and B. The end result of this operation is an array of the same size as A and B (whose sizes must be equal), where the jj'th element of the array is equal to A(jj)*B(jj).
Now, consider your definition of fun:
fun = #(x1,x2,x3) (x2-x1^2)^2+(1-x1)^2 + x3;
What happens when MATLAB evaluates this expression is that it applies the matrix operations, such as ^ to the input arrays. However, to obtain your desired result of applying the operation to every individual element in your input arrays x1, x2, you should be using element-wise operations.
A new definition
fun = #(x1,x2,x3) (x2-x1.^2).^2+(1-x1).^2 + x3;
should provide the desired result.

Polynomial expansion: Separating polynomial coefficients and x's

I want to automatically calculate expansions of polynomials where there are variables (x1,x2,...) as well as coefficients (c1,c2, ...). My goal is to calculate p(1)*(c1*x1+c2*x2+...)^n+ ... + p(n)*(c1*x1+c2*x2+...)^n .
As you can notice the resulting expression can be written as F(x1,x2...)*g(c1,c2,...) [where F is a row matrix and g is column matrix], i.e. there is some multiplicative decoupling between the coefficients and the variables.
Right now I use the MATLAB symbolic toolbox and construct F and g by manually examining the resulting symbolic expansions. This is not very feasible as if n is big and c=(c1,c2,...) is too big there are too many terms and it is no longer possible manually. For instance for (c1*x1+c2*x2+c3) and n=2, what I want is following.
>> p=[2 5]
p =
2 5
>> syms c1 c2 c3
>> syms x1 x2
>> expression= p(1)*(c1*x1+c2*x2+c3)^2 + p(2)*(c1*x1+c2*x2+c3);
>> expand(expression)
ans =
2*c1^2*x1^2 + 4*c1*c2*x1*x2 + 4*c1*c3*x1 + 5*c1*x1 + 2*c2^2*x2^2 + 4*c2*c3*x2 + 5*c2*x2 + 2*c3^2 + 5*c3
>> F=[5*x1 5*x2 5 4*x1*x2 4*x1 4*x2 2*x1^2 2*x2^2 2]
F =
[ 5*x1, 5*x2, 5, 4*x1*x2, 4*x1, 4*x2, 2*x1^2, 2*x2^2, 2]
>> g=[c1 c2 c3 c1*c2 c1*c3 c2*c3 c1^2 c2^2 c3^2].'
g =
c1
c2
c3
c1*c2
c1*c3
c2*c3
c1^2
c2^2
c3^2
>> expand(F*g)
ans =
2*c1^2*x1^2 + 4*c1*c2*x1*x2 + 4*c1*c3*x1 + 5*c1*x1 + 2*c2^2*x2^2 + 4*c2*c3*x2 + 5*c2*x2 + 2*c3^2 + 5*c3
I have found the following question and it looks like there may be a way to do it automatically using conv etc. If one can come up with an automated solution (or at least some idea towards such automation) for the case where x=(x1,x2) and c=(c1,c2,c3) and n=2, the case depicted above; I guess I may be able to generalize it to higher dimensional cases.
Note: the ordering of terms in F or g does not matter, given that they are ordered in some structured way.
The coefficients from different terms don't overlap. The first term, p(1)*(c'*x)^1, has only terms of degree 1 in xi and ci, and so on. So it becomes a matter of computing the coefficients of one term at a time.
That, too, has a "simple" expression:
p(k)*(c'*x)^k = sum(i1,..,im>=0 with sum(i_)=k)
M(k;i1,..,im)*x1^i1*...*xm^im * c1^i1*...*cm^im
where the summation is such that the sum of all i equals k, and M is the multinomial coefficient.
For m=3, n=2, the i's would be in the order of your example: 110,101,011,200,020,002. M(2;110)=2 so the first term is `p(2)*M(2;110)*x1*x2 = 4*x1*x2'.
Your F and g terms are:
F(...) = p(k)*M(k;i1,..,im)*x1^i1*...*xm^im
g(...) = c1^i1*...*cm^im