how to plot equation in matlab

How to Plot Equations in MATLAB

By Mark Robinson
AD

Matlab, a high-level language and interactive environment, provides a robust namespace for mathematical computing that includes features for visualization and data analysis. If you're eager to learn the intricacies of plotting equations in Matlab, you've come to the right place. Let's delve into this topic with enthusiasm.

Before we get started, ensure you have installed Matlab and are familiar with its basic functionalities. We'll cover plotting basic equations, but first, let's explore some fundamental principles.

Understanding the plot Function

The plot function in Matlab is the cornerstone of data visualization. It's a versatile tool that allows you to create numerous types of plots by switching the number and type of inputs.

AD
How to plot a function in Matlab
How to plot a function in Matlab
MATLAB Plot Gallery
MATLAB Plot Gallery
MATLAB - Plot Expression or Function
MATLAB - Plot Expression or Function

One of its most basic uses involves plotting a single input: a vector or an array. When plotting with no additional arguments, Matlab employs its default heavier-between-orthogonal-projections ordinate system, with x ranging from 1 to the length of the input, often represented as [1:N].

Plotting Equations in One Variable

To plot an equation of a single variable, you'll first need to evaluate it over a suitable range. Assume you wish to plot the function y=2*x+3; for x in the range [-5,5]. You can achieve this with the following code:

Graph | Plotting of mathematical functions
Graph | Plotting of mathematical functions
```matlab x = -5:0.1:5; y = 2*x + 3; plot(x, y); ```

The 'x' vector comprises 101 elements, each 0.1 apart, allowing for intricate visualization. The 'plot' function subsequently generates the graph.

Plotting Pie Charts

A unique feature of Matlab's plot function is its ability to generate pie charts, merely by specifying a single vector of numeric values as an input. The number of values in your vector defines the number of slices in the pie. The size of each value determines the proportion of the slice. Let's create a simple pie chart portraying months of the year and their average daily temperatures (in Celsius):

AD
```matlab temp = [15.1, 15.4, 11.0, 8.8, 12.1, 15.4, 18.3, 20.1, 17.4, 13.8, 14.6, 14.0]; plot(temp, '.-'); grid off; title('Average Daily Temperature (Celsius) by Month'); ```

This code will produce a pie chart depicting months and their average daily temperatures. The '.-' argument makes it brighter and more appealing to the eye. The 'grid off' command removes gridlines for clarity, and the 'title' command appends a title to the plot.

Utilizing Hold and Multiple Plots in Matlab

You'll frequently need to plot multiple functions or equations on the same axis. Matlab's 'hold' command helps, allowing you to overlay multiple graphs with relative ease.

To utilize this function, plot each equation separately, and Matlab will append the plots onto the same axes. Once all are complete, turn off the 'hold' function, and Matlab will display all your plots simultaneously.

AD

Combining Different Plot Types

Matlab doesn't restrict you to a single plot type. You can combine line, stem, plotted markers, bar, pie, histogram, and more. Let's add a histogram of temperatures to our current plot.

```matlab histogram(temp, 'FaceColor', 'b'); ```

The 'histogram' function creates a histogram within your existing plot, with blue ('b') vertical bars.

Learning to plot equations in Matlab opens a universe of possibilities for visualizing data and insights. Heretofore, armed with this newfound skill, you're primed for a future of comprehensive data visualization and exploration. Embark on that journey with confidence and creativity.

AD
AD