当前位置:网站首页>Matlab - polynomial and function

Matlab - polynomial and function

2022-06-09 05:31:00 kaims

polynomial

The representation of polynomials

One p A polynomial of order can be expressed as a polynomial containing p+1 Vector representation of elements ,MATLAB Denotes that a polynomial is a row vector containing coefficients arranged by descending powers , for example

p(x)=2*x^2+1

It can be expressed as

p = [2 0 1];

conv

Multiplication of polynomials .

p1=[1 1];% It is x+1 The coefficient matrix of 
p2=[1 0];% It is x The coefficient matrix of 
P=conv(p1,p2)
P=
    1    1    0
%P=[1 1 0], Explain that the multiplied polynomial is x^2+x

poly

  • Construct polynomials with roots . Such as x The root of a polynomial ,**poly(x)** The coefficients of the polynomial are obtained ;
  • Generating characteristic polynomials of matrices . Such as A Is a matrix ,**poly(A)** The coefficients of the characteristic polynomial of the matrix can be obtained ;

polyval

y=polyval(p,x);% According to the polynomial coefficient matrix p Seek for x Place the value of the 

roots

x=roots(p);% Find a polynomial p The root of the 

polyder

polyder(p);% For polynomials p Derivation 

polyfit

p = polyfit(x,y,n);% According to the data point set (x,y) Fit a polynomial , The highest power of a polynomial is n, Returns the polynomial coefficient matrix 

sym2poly

Through function expression ( Symbolic functional form ) Generate coefficient matrix

p=sym2poly(f);

poly2sym

The expression of polynomial function is generated by coefficient matrix ( Symbolic functional form )

f=poly2sym(p);

Symbolic function (Symbolic Function)

notes : Under this section matlab Code shares variables

adopt syms Defining variables , Write function expressions directly , This defines a symbolic function y

syms p q x y
p=x^2;
q=x^2+y^2;

The symbolic function image can be drawn through the function fplot( A two-dimensional ),ezplot( A two-dimensional ),ezsurf( The three dimensional ),ezmesh( The three dimensional )

ezplot(p);% Or define the scope ezplot(p,[-5,5]);
fplot(p);% ditto 
ezsurf(q);% Or define the scope ezsurf(q,[-1,1]); perhaps ezsurf(q,[-1,1],[-1,1]); 
ezmesh(q);% ditto 

Find the value of a symbolic function at a certain place ,

  • One way is through a function matlabFunction Turn a symbolic function into an anonymous function , Then calculate the function value
f1=matlabFunction(p)
f2=matlabFunction(q)
f1(3)
f2(1,2)
% The result is :
f1 = 
    @(x)x.^2
f2 = 
    @(x,y)x.^2+y.^2
ans =
     9
ans = 
     5

attach : Conversion between anonymous function and symbolic function

The above is about the conversion of symbolic functions to anonymous functions , Let's look at the conversion of anonymous functions to symbolic functions

[email protected](t)t.^2;
syms t
f4=f3(t)    %f4 Is the converted symbolic function 
  • Another way is through the function eval Directly find the value of a symbolic function at a certain place , First of all, I need to give x Assign a specific value
x=1;y=2;
eval(p)
eval(q)
% The result is 
ans = 
     1
ans = 
     5
  • Another way is through the function subs Directly find the value of a symbolic function at a certain place
subs(p,1)      %subs(p,x,1)
subs(q,[1 2])  %subs(q,[x y],[1 2])
% The result is 
ans = 
     1
ans = 
     5
% in addition ,subs(q,y,2) You'll get x^2+4

String function

  • String to function

str2func

fh = str2func(str)
% Constructor handles based on function names or textual representations of anonymous functions  fh
  • Function to string

func2str

c = func2str(fh)
% Construct character vectors  c, The character vector contains the function handle  fh  The name of the associated function . If  fh  Associated with anonymous functions , be  %func2str  Returns the character vector representing the anonymous function .

Anonymous functions (Anonymous Function)

Find the zero of the function

roots

Find the zero point function by polynomial

fzero

Find a numerical solution near a given initial value / Returns the zero point of a unary function in an interval

% Invocation format :
x=fzero(fun,x0) %fun Is a function that defines an equation or an anonymous function ;x0 Is a value or interval , As an initial condition 

We can only find a zero point in the interval , And the function value at the end of a given interval is required to have a different sign , So you should draw before using , Get the interval of single zero distribution , Then use this function to find the zero point . If there are multiple zeros , You need to use this function more than once .

fminbnd

Find the minimum value of a unary function in a certain interval and the corresponding minimum value point .

% Invocation format :
[x0,fmin]=fminbnd(fun,a,b);%fun Is a function that defines an equation or an anonymous function ; Interval is [a b]

solve

syms x
solve(2*x==1,x)   % It's not recommended right now solve('2*t=1','t')
% The result is 
ans =
     1/2
原网站

版权声明
本文为[kaims]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090517442467.html