Create Math Graph using Matlab?

In summary: If x and y are different lengths, then you can use the commands x=x'*y or y=x'*(1-y).In summary, the code above creates a vector named "x" with initial value -5 and final value 5, and calculates x^2+x+1 for every element in "x". The "plot" command can be used to visualize this vector.
  • #1
basty
95
0
How do I create a graph of y = x^2 + x + 1 in Matlab?

Where I could create the above math graph in Matlab (please see below image)?

Matlab.png
 
Physics news on Phys.org
  • #2
Type in command window the following:

Code:
x=[-5:0.2:5];
y=x.*x+2*x+1;
plot(x,y);
 
  • #3
soarce said:
Type in command window the following:

Code:
x=[-5:0.2:5];
y=x.*x+2*x+1;
plot(x,y);

What is the meaning of below code?

x=[-5:0.2:5];

Also, is y=x.*x+2*x+1; mean ##y = x^2 + 2x + 1##?

If so, then there is a mistake with the above code.

It should be ##y = x^2 + x + 1##.

What is the correct code of ##y = x^2 + x + 1## in Matlab?
 
  • #4
You can remove the factor of 2, I put it by mistake.

Anyway, you would better start a free tour of Matlab documentation.
 
  • #5
soarce said:
You can remove the factor of 2, I put it by mistake.

Anyway, you would better start a free tour of Matlab documentation.

Thank you, but, the documentation doesn't help.

Can someone please tell me the order of creating math graph in Matlab with explanation?
 
  • #7
I agree that taking the MATLAB tutorials would be helpful for you. These are basic syntax questions.

basty said:
What is the meaning of below code?
x=[-5:0.2:5];
This command tells MATLAB to create a vector called "x" with initial value -5 and final value 5 with intermediate values spaced .02 units apart.
You can adjust your initial and final values as well as your step size as you see fit.
y = x . ^2 + x + 1;
will create a vector called "y" which is the same size as x and for every value in x, it will compute x^2 + x +1. Notice the dot before the exponent: .^2 means to return the square of each element...as opposed to ^2 which tries to square the vector using matrix multiplication.

plot(x,y) is the simplest graphing option and requires that the two vectors be the same length.
 
  • Like
Likes kreil

1. How do I create a basic graph using Matlab?

To create a basic graph using Matlab, you can use the "plot" function. This function takes in two arrays of data, one for the x-axis and one for the y-axis, and plots them on a coordinate plane. You can also add labels, titles, and customize the appearance of the graph using additional parameters.

2. Can I plot multiple lines on the same graph in Matlab?

Yes, you can plot multiple lines on the same graph in Matlab by using the "hold on" command. This allows you to add multiple plots to the same figure, making it easier to compare and analyze different data sets.

3. How can I save a graph created in Matlab as an image?

To save a graph as an image in Matlab, you can use the "saveas" function. This function takes in the figure handle and the desired file format (such as PNG or JPEG) as parameters, and saves the graph as an image file in the current working directory.

4. Is it possible to add a legend to a Matlab graph?

Yes, you can add a legend to a Matlab graph by using the "legend" function. This function takes in a cell array of strings as a parameter, with each string representing the label for a different line on the graph. You can also customize the placement, font, and other properties of the legend using additional parameters.

5. Can I create different types of graphs in Matlab, such as bar graphs or pie charts?

Yes, Matlab offers a variety of functions for creating different types of graphs, including bar graphs, pie charts, histograms, and more. These functions have different parameters and syntax, so it's important to refer to the Matlab documentation for specific instructions on how to create the desired type of graph.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
140
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
819
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top