Detailed explanation of MATLAB drawing function fplot

Detailed explanation of the MATLAB drawing function fplot
1. The basic syntax of fplot
fplot is different from plot. It is mainly used to directly draw the function curve according to the function expression and the interval to which the independent variable belongs. It does not need to give the array of independent variables and dependent variables like plot needs to give , so when the function expression is known, it is relatively simple to use fplot to draw the function curve.
Its basic syntax is as follows:

1)fplot( f, xinterval, s )

Among them, f is the expression about the independent variable in the function, xinterval is the value range of the independent variable, and s represents the attribute of the primitive, which is similar to the attribute of the primitive in the plot. When xinterval is defaulted, the default interval of the independent variable is [-5, 5].

2)fplot( fx, fy, tinterval, s )

This form is mainly used to draw function curves represented by parametric equations. Among them, fx and fy respectively represent the expressions of x and y with respect to parameter t, tinterval is the value range of parameter t, and s represents the attribute of the primitive.

3)fplot( @(var) f(var), xinterval, s )

Among them, @(var) is to declare var as an independent variable (the identifier can be given as required), f(var) is a specific function expression, xinterval is the value range of the independent variable, and s indicates the attribute of the primitive.

4)fplot( @(t)fx(t), @(t) fy(t), tinterval, s )

Among them, @(t) is to declare t as a parameter (the identifier can be given as required), fx(t) and fy(t) are the expressions of the abscissa and ordinate of the specific parameter equation, and tinterval is the selection of the parameter t Value range, s represents the attribute of the primitive.
Note: Usage 1) and 2) will have warning prompts in the new version. 3) and 4) are standard usage in newer versions.
2. Specific examples
Example 1. Draw the y=sin(x) curve.

%示例代码(1)
clear all
clc
fplot( 'sin(x)' ) %只给定了函数表达式

%operation result
insert image description here

%示例代码(2)
clear all
clc
fplot( 'sin(x)',[ -pi, pi ], 'ro' ) 
xlabel( 'x' );
ylabel( 'sin(x)' );

%operation result
insert image description here

%示例代码(3)
clear all
clc
fplot( @(x)sin(x),[ -pi, pi ] ) 
xlabel( 'x' );
ylabel( 'sin(x)' );

%Running results
insert image description here
Example 2. Draw a sine curve and a cosine curve within a period in the same window

%示例代码
clear all
clc
fplot( @(x)sin(x),[ -pi, pi ], 'r-.' ) 
hold on
fplot( @(x)cos(x),[ -pi, pi ], 'b--' ) 
xlabel( 'x' );
ylabel( 'y' );
legend( 'y=sin(x)', 'y=cos(x)' );

%operation result
insert image description here

Example 3. Drawing the unit circle

%示例代码
clear all
clc
fplot( @(t)sin(t), @(t)cos(t),[ -pi, pi ] ) %实线单位圆
hold on
fplot( @(t)sin(t), @(t)cos(t),[ -pi, pi ], 'ro' ) %单位圆的散点图
xlabel( 'x' );
ylabel( 'y' );
title( 'Unit Circle' );
axis equal
axis( [ -1.5, 1.5, -1.5, 1.5 ] );

%operation result
insert image description here

Example 4. Plotting a piecewise function curve
insert image description here

%示例代码
clear all
clc
fplot( @(x)(7-x).^2/4,[1, 5 ], 'r' )
hold on
fplot( @(x)x-4,[5, 10 ], 'r' )
fplot( @(x)16-x,[10, 15 ], 'r' )
fplot( @(x)(x-13).^2/4,[15, 19 ], 'r' )
xlabel( 'x' );
ylabel( 'y' );
title( 'Piecewise Function Curve' );
axis( [ 0, 20, 0, 10 ] );

%operation result
insert image description here

Guess you like

Origin blog.csdn.net/sunnyoldman001/article/details/125724159