Create Math Graph using Matlab?

  • Context: MATLAB 
  • Thread starter Thread starter basty
  • Start date Start date
  • Tags Tags
    Graph Matlab
Click For Summary

Discussion Overview

The discussion revolves around creating a mathematical graph of the function y = x^2 + x + 1 using Matlab. Participants explore the syntax and commands necessary for plotting in Matlab, addressing both the creation of the graph and the interpretation of specific code segments.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant asks how to create a graph of y = x^2 + x + 1 in Matlab.
  • Another participant provides a code snippet but includes an incorrect term, suggesting y = x^2 + 2x + 1 instead.
  • A later reply questions the meaning of the code x = [-5:0.2:5] and seeks clarification on the correct formulation of the function.
  • Some participants acknowledge the mistake in the code and suggest removing the factor of 2.
  • There are suggestions for the original poster to consult Matlab documentation or tutorials for further guidance.
  • One participant explains the function of the dot operator in Matlab, indicating its importance in element-wise operations.

Areas of Agreement / Disagreement

Participants generally agree on the need for clarification regarding the Matlab code and the importance of using correct syntax. However, there is no consensus on the best approach to learning Matlab, as some suggest documentation while others recommend tutorials.

Contextual Notes

There are unresolved issues regarding the correct formulation of the mathematical expression in Matlab and the interpretation of specific commands. The discussion reflects varying levels of familiarity with Matlab syntax among participants.

Who May Find This Useful

Individuals learning Matlab, particularly those interested in graphing mathematical functions or seeking help with basic syntax and commands.

basty
Messages
94
Reaction score
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
Type in command window the following:

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

Anyway, you would better start a free tour of Matlab documentation.
 
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?
 
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   Reactions: kreil

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
7
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K