Quick Start with MATLABA (6): Numerical Calculus

Numerical Calculus

1. Numerical differentiation

dx=diff(x): Calculate the forward difference of the vector x

dx=diff(x,n): calculates the nth order forward difference of the vector x

dx=diff(A,n,dim): Calculate the n-order difference of matrix A. When dim is equal to 1 (default state), the difference is calculated by column. When dim is equal to 2, the difference is calculated by row.

2. Numerical integration

Based on adaptive Simpson method

[I,n]=quad(filename,a,b,tol,trace)

Based on adaptive Gauss-Lobatoo method

[I,n]=quadl(filename,a,b,tol,trace)

Based on global adaptive integration method

I=integral(filename,a,b)

filename is the name of the integrand; a and b are the upper limit and lower limit of the integral respectively, tol is used to control the integral precision,

trace controls whether to show the integration process, if it is not 0, it will show the integration process, if it is 0, it will not show, the default trace=0; the return parameter I is the value of the definite integral, and n is the number of calls of the integrand function.

Based on the adaptive Gauss-Cronrod method

[I,err]=quadgk(filename,a,b)

err returns the approximate margin of error.

Based on trapezoidal integration

I=trapz(x,y)

or I=sum(diff(x).*(y(1:end-1)+y(2:end))/2)

Among them, the vector x, y defines the functional relationship y=f(x),

gif.latex?I%3D%5Csum_%7Bi%3D1%7D%5E%7Bn-1%7Dh_%7Bi%7D%5Cfrac%7By_%7Bi+1%7D+y_%7Bi%7D%7D%7B2%7D

in,gif.latex?h_%7Bi%7D%3Dx_%7Bi+1%7D-x_%7Bi%7D

Numerical solution of the double integral:

I=integral2(filename,a,b,c,d)

I=quad2d(filename,a,b,c,d)

I=dblquad(filename,a,b,c,d,tol)

Numerical solution of the triple integral:

I=integral3(filename,a,b,c,d,e,f)

I=triplequad(filename,a,b,c,d,e,f,tol)

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZGItZG9n,size_20,color_FFFFFF,t_70,g_se,x_16

3. Numerical solution of nonlinear equations

3.1 Single variable nonlinear equation solution

X=fzero(filename,x0)

Among them, filename is the function expression on the left side of the equation to be found, and x0 is the initial value

X=fsolve(filename,x0,option)

option is used to set the optimization parameters of the optimization toolbox, which can be done with the optimest function

 watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZGItZG9n,size_20,color_FFFFFF,t_70,g_se,x_16

 3.2 Unconstrained optimization problem

[xmin,fmin]=fminbnd(filename,x1,x2,option)

[xmin,fmin]=fminsearch(filename,x0,option)

[xmin,fmin]=fminunc(filename,x0,option)

Among them, xmin represents the minimum value point, fmin represents the minimum value, x1, x2 represent the left and right boundaries of the studied interval, and x0 is a vector representing the initial value of the extreme value point.

3.3 Functions with constrained minimum values

[xmin,fmin]=fmincon(filename,x0,A,b,Aeq,beq,Lbnd,Ubnd,NonF,option)

A, b, Aeq, beq, Lbnd, Ubnd, NonF are constraints

 watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZGItZG9n,size_20,color_FFFFFF,t_70,g_se,x_16

4. Numerical solution of ordinary differential equations

The call format is [t,y]=solver(filename,tspan,option), where t and y respectively give the time vector and the corresponding numerical solution, filename is the name of the function defining f(t,y), and the function must return A column vector, tspan is in the form of [t0,tf], which represents the solution interval, and y0 is the initial state vector.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZGItZG9n,size_18,color_FFFFFF,t_70,g_se,x_16

The solution results are plotted as follows:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZGItZG9n,size_16,color_FFFFFF,t_70,g_se,x_16

 


 

Scan the QR code to follow the official account " Talk to You ", and reply to the keyword " Getting Started " in the background of the official account , and you can get the full version of the MATLAB PDF document!

 

Guess you like

Origin blog.csdn.net/m0_64087341/article/details/123365074