Matlab x 2

Matlab x 2 DEFAULT

How can I plot a sin (x^2) function

First, there is ABSOLUTELY NO reason to predefine x as a sym. So this line is completely irrelevant:

syms x;

When you do create x, you overwrite the symbolic version of x that you created before anyway.

Next, you created a function

[email protected](x)sin(x.^2);

Good there. But then what?

What do you think this does?

x=[-pi pi];

It does NOT create the interval [-pi,pi]. Instead, it creates a vector of length 2, so TWO values, -pi and pi. Then when you plotted, using

plot(x,f(x))

it plots TWO points, connected with a straight line. And since the two poiints each had function value of zero, the line is perfectly horizontal. Compare that to this version:

[email protected](x)sin(x.^2);

x=linspace(-pi,pi,100);

plot(x,f(x))

grid

See that I never needed to pre-define x as symbolic. (Why would you want to do that anyway? Just because you don't know the value of something, does not mean it must automatically be symbolic. This is perhaps the one of most common mistakes I see made by new users.)

Simpler yet, you might have done just this:

f = @(x) sin(x.^2);

fplot(f,[-pi,pi])

grid

Sours: https://www.mathworks.com/matlabcentral/answers/480697-how-can-i-plot-a-sin-x-2-function

MATLAB - Plotting



To plot the graph of a function, you need to take the following steps −

  • Define x, by specifying the range of values for the variable x, for which the function is to be plotted

  • Define the function, y = f(x)

  • Call the plot command, as plot(x, y)

Following example would demonstrate the concept. Let us plot the simple function y = x for the range of values for x from 0 to 100, with an increment of 5.

Create a script file and type the following code −

x = [0:5:100]; y = x; plot(x, y)

When you run the file, MATLAB displays the following plot −

Plotting y = x

Let us take one more example to plot the function y = x2. In this example, we will draw two graphs with the same function, but in second time, we will reduce the value of increment. Please note that as we decrease the increment, the graph becomes smoother.

Create a script file and type the following code −

x = [1 2 3 4 5 6 7 8 9 10]; x = [-100:20:100]; y = x.^2; plot(x, y)

When you run the file, MATLAB displays the following plot −

Plotting y = x^2

Change the code file a little, reduce the increment to 5 −

x = [-100:5:100]; y = x.^2; plot(x, y)

MATLAB draws a smoother graph −

Plotting y = x^2 with less increment

Adding Title, Labels, Grid Lines and Scaling on the Graph

MATLAB allows you to add title, labels along the x-axis and y-axis, grid lines and also to adjust the axes to spruce up the graph.

  • The xlabel and ylabel commands generate labels along x-axis and y-axis.

  • The title command allows you to put a title on the graph.

  • The grid on command allows you to put the grid lines on the graph.

  • The axis equal command allows generating the plot with the same scale factors and the spaces on both axes.

  • The axis square command generates a square plot.

Example

Create a script file and type the following code −

x = [0:0.01:10]; y = sin(x); plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'), grid on, axis equal

MATLAB generates the following graph −

Sprucing up our graphs

Drawing Multiple Functions on the Same Graph

You can draw multiple graphs on the same plot. The following example demonstrates the concept −

Example

Create a script file and type the following code −

x = [0 : 0.01: 10]; y = sin(x); g = cos(x); plot(x, y, x, g, '.-'), legend('Sin(x)', 'Cos(x)')

MATLAB generates the following graph −

Multiple Functions on the Same Graph

Setting Colors on Graph

MATLAB provides eight basic color options for drawing graphs. The following table shows the colors and their codes −

CodeColor
wWhite
kBlack
bBlue
rRed
cCyan
gGreen
mMagenta
yYellow

Example

Let us draw the graph of two polynomials

  • f(x) = 3x4 + 2x3+ 7x2 + 2x + 9 and

  • g(x) = 5x3 + 9x + 2

Create a script file and type the following code −

x = [-10 : 0.01: 10]; y = 3*x.^4 + 2 * x.^3 + 7 * x.^2 + 2 * x + 9; g = 5 * x.^3 + 9 * x + 2; plot(x, y, 'r', x, g, 'g')

When you run the file, MATLAB generates the following graph −

Colors on Graph

Setting Axis Scales

The axis command allows you to set the axis scales. You can provide minimum and maximum values for x and y axes using the axis command in the following way −

axis ( [xmin xmax ymin ymax] )

The following example shows this −

Example

Create a script file and type the following code −

x = [0 : 0.01: 10]; y = exp(-x).* sin(2*x + 3); plot(x, y), axis([0 10 -1 1])

When you run the file, MATLAB generates the following graph −

Setting Axis Scales

Generating Sub-Plots

When you create an array of plots in the same figure, each of these plots is called a subplot. The subplot command is used for creating subplots.

Syntax for the command is −

subplot(m, n, p)

where, m and n are the number of rows and columns of the plot array and p specifies where to put a particular plot.

Each plot created with the subplot command can have its own characteristics. Following example demonstrates the concept −

Example

Let us generate two plots −

y = e−1.5xsin(10x)

y = e−2xsin(10x)

Create a script file and type the following code −

x = [0:0.01:5]; y = exp(-1.5*x).*sin(10*x); subplot(1,2,1) plot(x,y), xlabel('x'),ylabel('exp(–1.5x)*sin(10x)'),axis([0 5 -1 1]) y = exp(-2*x).*sin(10*x); subplot(1,2,2) plot(x,y),xlabel('x'),ylabel('exp(–2x)*sin(10x)'),axis([0 5 -1 1])

When you run the file, MATLAB generates the following graph −

Generating Sub-Plots
Sours: https://www.tutorialspoint.com/matlab/matlab_plotting.htm
  1. Stock video water
  2. Erie county muni
  3. Kirito vs alice

plot

Main Content

  • 2-D line plot

Syntax

Description

example

creates a 2-D line plot of the data in versus the corresponding values in .

  • To plot a set of coordinates connected by line segments, specify and as vectors of the same length.

  • To plot multiple sets of coordinates on the same set of axes, specify at least one of or as a matrix.

creates the plot using the specified line style, marker, and color.

example

plots multiple pairs of x- and y-coordinates on the same set of axes. Use this syntax as an alternative to specifying coordinates as matrices.

example

assigns specific line styles, markers, and colors to each x-y pair. You can specify for some x-y pairs and omit it for others. For example, specifies markers for the first x-y pair but not for the second pair.

example

plots against an implicit set of x-coordinates.

  • If is a vector, the x-coordinates range from 1 to .

  • If is a matrix, the plot contains one line for each column in . The x-coordinates range from 1 to the number of rows in .

If contains complex numbers, MATLAB® plots the imaginary part of versus the real part of . If you specify both and , the imaginary part is ignored.

specifies line style, marker, and color.

example

specifies properties using one or more name-value arguments. The properties apply to all the plotted lines. Specify the name-value arguments after all the arguments in any of the previous syntaxes. For a list of properties, see Line Properties.

example

displays the plot in the target axes. Specify the axes as the first argument in any of the previous syntaxes.

example

returns a object or an array of objects. Use to modify properties of the plot after creating it. For a list of properties, see Line Properties.

Examples

collapse all

Create Line Plot

Create as a vector of linearly spaced values between 0 and . Use an increment of between the values. Create as sine values of . Create a line plot of the data.

x = 0:pi/100:2*pi; y = sin(x); plot(x,y)

Plot Multiple Lines

Define as 100 linearly spaced values between and . Define and as sine and cosine values of . Create a line plot of both sets of data.

x = linspace(-2*pi,2*pi); y1 = sin(x); y2 = cos(x); figure plot(x,y1,x,y2)

Create Line Plot From Matrix

Define as the 4-by-4 matrix returned by the function.

Y = 4×4 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

Create a 2-D line plot of . MATLAB® plots each matrix column as a separate line.

Specify Line Style

Plot three sine curves with a small phase shift between each line. Use the default line style for the first line. Specify a dashed line style for the second line and a dotted line style for the third line.

x = 0:pi/100:2*pi; y1 = sin(x); y2 = sin(x-0.25); y3 = sin(x-0.5); figure plot(x,y1,x,y2,'--',x,y3,':')

MATLAB® cycles the line color through the default color order.

Specify Line Style, Color, and Marker

Plot three sine curves with a small phase shift between each line. Use a green line with no markers for the first sine curve. Use a blue dashed line with circle markers for the second sine curve. Use only cyan star markers for the third sine curve.

x = 0:pi/10:2*pi; y1 = sin(x); y2 = sin(x-0.25); y3 = sin(x-0.5); figure plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')

Display Markers at Specific Data Points

Create a line plot and display markers at every fifth data point by specifying a marker symbol and setting the property as a name-value pair.

x = linspace(0,10); y = sin(x); plot(x,y,'-o','MarkerIndices',1:5:length(y))

Specify Line Width, Marker Size, and Marker Color

Create a line plot and use the option to specify a dashed green line with square markers. Use pairs to specify the line width, marker size, and marker colors. Set the marker edge color to blue and set the marker face color using an RGB color value.

x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); figure plot(x,y,'--gs',...'LineWidth',2,...'MarkerSize',10,...'MarkerEdgeColor','b',...'MarkerFaceColor',[0.5,0.5,0.5])

Add Title and Axis Labels

Use the function to define as a vector of 150 values between 0 and 10. Define as cosine values of .

x = linspace(0,10,150); y = cos(5*x);

Create a 2-D line plot of the cosine curve. Change the line color to a shade of blue-green using an RGB color value. Add a title and axis labels to the graph using the , , and functions.

figure plot(x,y,'Color',[0,0.7,0.9]) title('2-D Line Plot') xlabel('x') ylabel('cos(5x)')

Plot Durations and Specify Tick Format

Define as seven linearly spaced values between 0 and 3 minutes. Plot random data and specify the format of the tick marks using the name-value pair argument.

t = 0:seconds(30):minutes(3); y = rand(1,7); plot(t,y,'DurationTickFormat','mm:ss')

Specify Axes for Line Plot

Starting in R2019b, you can display a tiling of plots using the and functions. Call the function to create a 2-by-1 tiled chart layout. Call the function to create an axes object and return the object as . Create the top plot by passing to the function. Add a title and y-axis label to the plot by passing the axes to the and functions. Repeat the process to create the bottom plot.

% Create data and 2-by-1 tiled chart layout x = linspace(0,3); y1 = sin(5*x); y2 = sin(15*x); tiledlayout(2,1) % Top plot ax1 = nexttile; plot(ax1,x,y1) title(ax1,'Top Plot') ylabel(ax1,'sin(5x)') % Bottom plot ax2 = nexttile; plot(ax2,x,y2) title(ax2,'Bottom Plot') ylabel(ax2,'sin(15x)')

Modify Lines After Creation

Define as 100 linearly spaced values between and . Define and as sine and cosine values of . Create a line plot of both sets of data and return the two chart lines in .

x = linspace(-2*pi,2*pi); y1 = sin(x); y2 = cos(x); p = plot(x,y1,x,y2);

Change the line width of the first line to 2. Add star markers to the second line. Use dot notation to set properties.

p(1).LineWidth = 2; p(2).Marker = '*';

Plot Circle

Plot a circle centered at the point (4,3) with a radius equal to 2. Use to use equal data units along each coordinate direction.

r = 2; xc = 4; yc = 3; theta = linspace(0,2*pi); x = r*cos(theta) + xc; y = r*sin(theta) + yc; plot(x,y) axis equal

Input Arguments

collapse all

x-coordinates
scalar | vector | matrix

x-coordinates, specified as a scalar, vector, or matrix. The size and shape of depends on the shape of your data and the type of plot you want to create. This table describes the most common situations.

Type of PlotHow to Specify Coordinates
Single point

Specify and as scalars and include a marker. For example:

One set of points

Specify and as any combination of row or column vectors of the same length. For example:

Multiple sets of points
(using vectors)

Specify consecutive pairs of and vectors. For example:

plot([1 2 3],[4 5 6],[1 2 3],[7 8 9])
Multiple sets of points
(using matrices)

If all the sets share the same x- or y-coordinates, specify the shared coordinates as a vector and the other coordinates as a matrix. The length of the vector must match one of the dimensions of the matrix. For example:

plot([1 2 3],[4 5 6; 7 8 9])
If the matrix is square, MATLAB plots one line for each column in the matrix.

Alternatively, specify and as matrices of equal size. In this case, MATLAB plots each column of against the corresponding column of . For example:

plot([1 2 3; 4 5 6],[7 8 9; 10 11 12])

Data Types: | | | | | | | | | | | |

y-coordinates
scalar | vector | matrix

y-coordinates, specified as a scalar, vector, or matrix. The size and shape of depends on the shape of your data and the type of plot you want to create. This table describes the most common situations.

Type of PlotHow to Specify Coordinates
Single point

Specify and as scalars and include a marker. For example:

One set of points

Specify and as any combination of row or column vectors of the same length. For example:

Alternatively, specify just the y-coordinates. For example:

Multiple sets of points
(using vectors)

Specify consecutive pairs of and vectors. For example:

plot([1 2 3],[4 5 6],[1 2 3],[7 8 9])
Multiple sets of points
(using matrices)

If all the sets share the same x- or y-coordinates, specify the shared coordinates as a vector and the other coordinates as a matrix. The length of the vector must match one of the dimensions of the matrix. For example:

plot([1 2 3],[4 5 6; 7 8 9])
If the matrix is square, MATLAB plots one line for each column in the matrix.

Alternatively, specify and as matrices of equal size. In this case, MATLAB plots each column of against the corresponding column of . For example:

plot([1 2 3; 4 5 6],[7 8 9; 10 11 12])

Data Types: | | | | | | | | | | | |

— Line style, marker, and color
character vector | string

Line style, marker, and color, specified as a character vector or string containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.

Example: is a red dashed line with circle markers

Line StyleDescriptionResulting Line
Solid line
Dashed line
Dotted line
Dash-dotted line
MarkerDescriptionResulting Marker
Circle
Plus sign
Asterisk
Point
Cross
Horizontal line
Vertical line
Square
Diamond
Upward-pointing triangle
Downward-pointing triangle
Right-pointing triangle
Left-pointing triangle
Pentagram
Hexagram
Color NameShort NameRGB TripletAppearance

— Target axes
object | object | object

Target axes, specified as an object, a object, or a object. If you do not specify the axes, MATLAB plots into the current axes or it creates an object if one does not exist.

To create a polar plot or geographic plot, specify as a or object. Alternatively, call the or function.

Name-Value Arguments

Specify optional comma-separated pairs of arguments. is the argument name and is the corresponding value. must appear inside quotes. You can specify several name and value pair arguments in any order as .

Example:

The chart line properties listed here are only a subset. For a complete list, see Line Properties.

— Line color
(default) | RGB triplet | hexadecimal color code | | | | ...

Line color, specified as an RGB triplet, a hexadecimal color code, a color name, or a short name.

For a custom color, specify an RGB triplet or a hexadecimal color code.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range ; for example, .

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol () followed by three or six hexadecimal digits, which can range from to . The values are not case sensitive. Thus, the color codes , , , and are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
Not applicableNot applicableNot applicableNo color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance

Example:

Example:

Example:

— Line style
(default) | | | |

Line style, specified as one of the options listed in this table.

Line StyleDescriptionResulting Line
Solid line
Dashed line
Dotted line
Dash-dotted line
No lineNo line

— Line width
(default) | positive value

Line width, specified as a positive value in points, where 1 point = 1/72 of an inch. If the line has markers, then the line width also affects the marker edges.

The line width cannot be thinner than the width of a pixel. If you set the line width to a value that is less than the width of a pixel on your system, the line displays as one pixel wide.

— Marker symbol
(default) | | | | | ...

Marker symbol, specified as one of the values listed in this table. By default, the object does not display markers. Specifying a marker symbol adds markers at each data point or vertex.

MarkerDescriptionResulting Marker
Circle
Plus sign
Asterisk
Point
Cross
Horizontal line
Vertical line
Square
Diamond
Upward-pointing triangle
Downward-pointing triangle
Right-pointing triangle
Left-pointing triangle
Pentagram
Hexagram
No markersNot applicable

— Indices of data points at which to display markers
(default) | vector of positive integers | scalar positive integer

Indices of data points at which to display markers, specified as a vector of positive integers. If you do not specify the indices, then MATLAB displays a marker at every data point.

Note

To see the markers, you must also specify a marker symbol.

Example: displays a circle marker at the first, fifth, and tenth data points.

Example: displays a cross marker every three data points.

Example: displays one square marker at the fifth data point.

— Marker outline color
(default) | RGB triplet | hexadecimal color code | | | | ...

Marker outline color, specified as , an RGB triplet, a hexadecimal color code, a color name, or a short name. The default value of uses the same color as the property.

For a custom color, specify an RGB triplet or a hexadecimal color code.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range ; for example, .

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol () followed by three or six hexadecimal digits, which can range from to . The values are not case sensitive. Thus, the color codes , , , and are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
Not applicableNot applicableNot applicableNo color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance

— Marker fill color
(default) | | RGB triplet | hexadecimal color code | | | | ...

Marker fill color, specified as , an RGB triplet, a hexadecimal color code, a color name, or a short name. The option uses the same color as the property of the parent axes. If you specify and the axes plot box is invisible, the marker fill color is the color of the figure.

For a custom color, specify an RGB triplet or a hexadecimal color code.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range ; for example, .

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol () followed by three or six hexadecimal digits, which can range from to . The values are not case sensitive. Thus, the color codes , , , and are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
Not applicableNot applicableNot applicableNo color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance

— Marker size
(default) | positive value

Marker size, specified as a positive value in points, where 1 point = 1/72 of an inch.

— Format for tick labels
character vector | string

Format for tick labels, specified as the comma-separated pair consisting of and a character vector or string containing a date format. Use the letters and to construct a custom format. These letters correspond to the Unicode® Locale Data Markup Language (LDML) standard for dates. You can include non-ASCII letter characters such as a hyphen, space, or colon to separate the fields.

If you do not specify a value for , then automatically optimizes and updates the tick labels based on the axis limits.

Example: displays a date and time such as .

The following table shows several common display formats and examples of the formatted output for the date, Saturday, April 19, 2014 at 9:41:06 PM in New York City.

Value of Example

For a complete list of valid letter identifiers, see the property for datetime arrays.

is not a chart line property. You must set the tick format using the name-value pair argument when creating a plot. Alternatively, set the format using the and functions.

The property of the datetime ruler stores the format.

— Format for tick labels
character vector | string

Format for tick labels, specified as the comma-separated pair consisting of and a character vector or string containing a duration format.

If you do not specify a value for , then automatically optimizes and updates the tick labels based on the axis limits.

To display a duration as a single number that includes a fractional part, for example, 1.234 hours, specify one of the values in this table.

Value of Description
Number of exact fixed-length years. A fixed-length year is equal to 365.2425 days.
Number of exact fixed-length days. A fixed-length day is equal to 24 hours.
Number of hours
Number of minutes
Number of seconds

Example: displays duration values in terms of fixed-length days.

To display a duration in the form of a digital timer, specify one of these values.

In addition, you can display up to nine fractional second digits by appending up to nine characters.

Example: displays the milliseconds of a duration value to three digits.

is not a chart line property. You must set the tick format using the name-value pair argument when creating a plot. Alternatively, set the format using the and functions.

The property of the duration ruler stores the format.

Tips

  • Use and values to create breaks in the lines. For example, this code plots the first two elements, skips the third element, and draws another line using the last two elements:

  • uses colors and line styles based on the and properties of the axes. cycles through the colors with the first line style. Then, it cycles through the colors again with each additional line style.

    Starting in R2019b, you can change the colors and the line styles after plotting by setting the or properties on the axes. You can also call the function to change the color order for all the axes in the figure.

Extended Capabilities

Tall Arrays
Calculate with arrays that have more rows than fit in memory.

Usage notes and limitations:

  • Supported syntaxes for tall arrays and are:

  • must be in monotonically increasing order.

  • Categorical inputs are not supported.

  • Tall inputs must be real column vectors.

  • With tall arrays, the function plots in iterations, progressively adding to the plot as more data is read. During the updates, a progress indicator shows the proportion of data that has been plotted. Zooming and panning is supported during the updating process, before the plot is complete. To stop the update process, press the pause button in the progress indicator.

For more information, see Visualization of Tall Arrays.

GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes and limitations:

  • This function accepts GPU arrays, but does not run on a GPU.

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

Usage notes and limitations:

  • This function operates on distributed arrays, but executes in the client MATLAB.

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

See Also

Functions

Properties

Introduced before R2006a

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web site

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

Sours: https://www.mathworks.com/help/matlab/ref/plot.html
Matlab Tutorial - 60 - Plotting Functions

Let's see what is hiding under your blouse. I saw two large breasts, size 6 or larger, with large nipples. I got up from the chair and walked over to the lady. Carefully Then get up and go out into the clearing.

X 2 matlab

And now, pumped up by the streams of his sperm, Ivanna did not even think to jump off the big, stretched out her tender bosom, a member. At home, two tenderly. Loving women were now waiting for him in truth.

Matlab for Non Believers - 3D Surface Plots

From our general stress and, due to her feminine insidiousness and general purely feminine malice, did not fail to pin up the gallant captain. In general, women have a more flexible psyche - having cried and hysterically to their heart's content, they clearly shed their negative emotions and then they can fully engage in business.

We, humble men, often keep all the negative energy inside ourselves and move away from stress for longer, although we try not to show it. Well, all right, emotions later, but now we need to do the improvement of our "camp", as Alina constantly jokes.

Now discussing:

All Nikolai wanted to promote was his cock. The guy really wanted to hold his own private show with Margarita, and preferably without a condom. It was graduation year.



1419 1420 1421 1422 1423