Need some help with matlab question

  • Thread starter magma_saber
  • Start date
  • Tags
    Matlab
In summary, the task was to plot the values of sine and cosine functions with t values between -pi and pi using linspace. The resulting plot appears as a circle with points ranging from -1 to 1 on both the x and y axes.
  • #1
magma_saber
73
0

Homework Statement


I have to make a plot with sin(t) (y values) vs cos(t) (x values). The values of t should be between -pi and pi. I'm supposed to use linspace.


Homework Equations





The Attempt at a Solution


i used these lines:

t=linspace(-pi,pi)
plot(cos(t),sin(t))

when i tried it out, the plot came out like this:
2pqnx5i.jpg


it shouldn't look like this should it? its only showing the plot go from -1 to 1.
 
Physics news on Phys.org
  • #2
magma_saber said:

Homework Statement


I have to make a plot with sin(t) (y values) vs cos(t) (x values). The values of t should be between -pi and pi. I'm supposed to use linspace.


Homework Equations





The Attempt at a Solution


i used these lines:

t=linspace(-pi,pi)
plot(cos(t),sin(t))

when i tried it out, the plot came out like this:
2pqnx5i.jpg


it shouldn't look like this should it? its only showing the plot go from -1 to 1.

If t is a real number, both sin t and cos t have values in the [-1, +1] interval, so your graph is correct (a circle).
 
  • #3


Hello,

Thank you for reaching out for help with your MATLAB question. It looks like you are on the right track with using linspace to create a vector of t values between -pi and pi. However, it seems like you may have forgotten to specify the number of points you want linspace to generate. By default, linspace will create 100 points between the specified range. In your case, you may want to increase the number of points to get a smoother plot.

Also, it looks like you may have missed the semicolon at the end of the first line of your code. This will prevent the vector t from being displayed in the command window and will make your code more readable.

Your code should look something like this:

t = linspace(-pi,pi,1000);
plot(cos(t),sin(t));

This will create 1000 points between -pi and pi and plot them as x and y values on a sine wave. Your plot should then look like a complete sine wave.

I hope this helps. Keep up the good work and don't hesitate to ask for help if you encounter any further difficulties.


 

1. How do I declare and initialize a variable in Matlab?

In Matlab, variables are declared and initialized by using the equal sign (=) operator. The variable name goes on the left side of the equal sign, and the value or expression to be assigned to the variable goes on the right side. For example, to declare and initialize a variable named "x" with a value of 5, you would write:

x = 5

Note: Matlab is case-sensitive, so "x" and "X" are considered different variables.

2. How can I solve a system of equations in Matlab?

To solve a system of equations in Matlab, you can use the "solve" function. The syntax for the function is:

solve(eq1, eq2, eq3, ...)

Where "eq1, eq2, eq3, ..." are the equations in the system. You can also use the "syms" function to declare the variables in the equations as symbolic variables before using the "solve" function. For more complex systems, you can also use the "fsolve" function.

3. How do I plot a graph in Matlab?

To plot a graph in Matlab, you can use the "plot" function. The syntax for the function is:

plot(x, y)

Where "x" and "y" are the variables or vectors containing the data points to be plotted. You can also customize the graph by adding a title, labels for the axes, and changing the style and color of the plot using additional arguments in the "plot" function.

4. How can I read data from a file in Matlab?

To read data from a file in Matlab, you can use the "readtable" or "csvread" functions. The "readtable" function is used for reading data from a text or Excel file, while the "csvread" function is used for reading data from a comma-separated values (CSV) file. Both functions return the data as a matrix, which can then be manipulated and used in your code.

5. How do I create a loop in Matlab?

To create a loop in Matlab, you can use the "for" or "while" statements. The syntax for a "for" loop is:

for i = start:increment:end
% code to be executed
end

Where "i" is the loop variable, "start" is the initial value of the loop variable, "increment" is the amount by which the loop variable is incremented in each iteration, and "end" is the final value of the loop variable. The syntax for a "while" loop is:

while condition
% code to be executed
end

Where "condition" is a logical expression that is evaluated in each iteration, and the loop continues as long as the condition is true.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
857
  • Engineering and Comp Sci Homework Help
Replies
1
Views
930
  • Engineering and Comp Sci Homework Help
Replies
1
Views
790
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
805
  • Engineering and Comp Sci Homework Help
Replies
0
Views
478
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
546
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
Back
Top