Create Math Graph using Matlab?

  • MATLAB
  • Thread starter basty
  • Start date
  • #1
95
0

Main Question or Discussion Point

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
 

Answers and Replies

  • #2
155
24
Type in command window the following:

Code:
x=[-5:0.2:5];
y=x.*x+2*x+1;
plot(x,y);
 
  • #3
95
0
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
155
24
You can remove the factor of 2, I put it by mistake.

Anyway, you would better start a free tour of Matlab documentation.
 
  • #5
95
0
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
RUber
Homework Helper
1,687
344
I agree that taking the MATLAB tutorials would be helpful for you. These are basic syntax questions.

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

Related Threads on Create Math Graph using Matlab?

  • Last Post
Replies
6
Views
9K
  • Last Post
Replies
2
Views
1K
Replies
1
Views
3K
Replies
1
Views
4K
Replies
3
Views
47K
Replies
3
Views
2K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
5
Views
67K
  • Last Post
Replies
2
Views
2K
Top