Plot the equation y=cos(A*x) + i*sin(A*x) in MATLAB

  • MATLAB
  • Thread starter mtirmize
  • Start date
  • Tags
    Matlab Plot
In summary, to plot the equation y=cos(A*x) + i*sin(A*x) in MATLAB, you can use the "plot" function and define values for A and x. The "i" in the equation represents the imaginary unit and allows for plotting as a complex valued function. To see different plots, you can change the values of A and x. This equation can also be plotted in 3D using the "plot3" function. To add a title and labels to the plot, use the "title", "xlabel", and "ylabel" functions after the "plot" function in your code.
  • #1
mtirmize
9
0
Hi Everyone,

I am a new Matlab User, I am want to plot the equation y=cos(A*x) + i*sin(A*x)

where A = 400*pi/340 and want to plot y and vary x from pi:0.1:pi

clear all;
close all;

A = 400*pi/340;
x = -180:0,1:180;


a=cos(A*x);
b=i*sin(A*x);

i= imag(b);

hold on;
grid on;

plot(a,i, 'r-*','MarkerEdgeColor','b');

xlabel('Distance');
ylabel('Primary Source');
title('Simulation Results');
 
Physics news on Phys.org
  • #2
You defined x in degrees, then used the radian trig functions. There are degree counterparts, like sind(x), that calculate the trig values in degrees. So choose one or the other, but make it consistent.

It's a best practice to use elementwise operations for your functions. Of course, the operators * and .* are the same when a scalar is involved, but it will save you headaches in the future.

Your call to plot confuses me. You are plotting i against a? I think this is why your current plot looks strange. You should be plotting a + i against x.


Code:
clear
clc

A = 400*pi/340;
x = -pi:0.1:pi;

y = cos(A.*x) + imag(i.*sin(A.*x));

hold on; grid on;
plot(x,y,'r-*','MarkerEdgeColor','b')
xlabel('Distance');
ylabel('Primary Source');
title('Simulation Results');
 

Attachments

  • untitled.png
    untitled.png
    4 KB · Views: 490
  • #3
thanks you so much for your quick reply

can you let me know if this is also correct ?

clear all;
close all;

A=400*pi/340;

theta= -2*pi:0.03:2*pi;

y=exp(1i*A*theta);


grid on;
hold on;

plot(theta,y, 'r-*','MarkerEdgeColor','b');


xlabel('Distance');
ylabel('Source');
title('Simulation Results');
 
  • #4
The only thing is that y = exp(1i*A*theta); yields a vector of complex numbers. If you care only about the real part, you should plot real(y), if you care only about the imaginary part, you should plot imag(y).

When you use plot(theta, y) it ignores all imaginary components.

HOWEVER, when you supply only a single imaginary argument, MATLAB plots the real part vs the imaginary part. For your second example, it produces the unit circle

Code:
clear all; close all;

A=400*pi/340;
theta= -2*pi:0.03:2*pi;

y=exp(1i*A*theta);

grid on; hold on;
plot(y, 'r-*','MarkerEdgeColor','b');
axis equal;
xlabel('Distance');
ylabel('Source');
title('Simulation Results');

Edit: I should have used different axis names
 

Attachments

  • untitled.png
    untitled.png
    3.7 KB · Views: 456
  • #5
okay i understand that now. I really appreciate your help and your time. God Bless you
 
  • #6
mtirmize said:
thanks you so much for your quick reply

can you let me know if this is also correct ?

Code:
clear all; 
close all;

A=400*pi/340;

theta= -2*pi:0.03:2*pi;

y=exp(1i*A*theta);
The line above bothers me. I'm not an expert in matlab, and don't have it to try things out, but no programming language that I'm familiar with allows a variable whose name starts with a decimal digit. I would be surprised to find that MATLAB allows a variable whose name is 1i or 2i or 3i, etc.

If your intent was 1 * i, that would be OK, but what's the point?
mtirmize said:
Code:
grid on;
hold on;

plot(theta,y, 'r-*','MarkerEdgeColor','b');xlabel('Distance');
ylabel('Source');
title('Simulation Results');

PS - instead of color tags, please use [code] tags around your code.
 
  • #7
Yes, 1i is "a thing" in MATLAB

Since "i" and "j" are functions in MATLAB for doing complex maths, when you use "i" or "j" in a loop, for example, you may be silently breaking code that relied on using them as complex. So one solution is to always use "1i" or "1j" for complex maths, leaving "i" and "j" open for use elsewhere.

To top it off, 1i and 1j are actually recommended in the documentation for improved speed with complex calculations.
 
Last edited:

1. How do I plot the equation y=cos(A*x) + i*sin(A*x) in MATLAB?

To plot this equation in MATLAB, you can use the "plot" function. First, define the values of A and x, and then use the "cos" and "sin" functions to calculate the corresponding y values. Finally, use the "plot" function to plot the values on a graph.

2. What is the purpose of using the "i" in the equation y=cos(A*x) + i*sin(A*x)?

The "i" in this equation represents the imaginary unit, which is used in complex numbers. In this case, it allows us to plot the equation as a complex valued function, where the real and imaginary parts are represented by the cosine and sine functions, respectively.

3. How can I change the values of A and x to see different plots?

You can change the values of A and x by simply redefining them in your code. This will result in different plots, as the values of A and x directly affect the values of y in the equation.

4. Can I plot this equation in 3D?

Yes, you can plot this equation in 3D by using the "plot3" function instead of the "plot" function. This will create a 3D graph with the x and y coordinates being the real and imaginary parts of the equation, and the z coordinate being the value of A.

5. How do I add a title and labels to the plot?

To add a title to the plot, you can use the "title" function and provide a string as the title. To add labels to the x and y axes, you can use the "xlabel" and "ylabel" functions, respectively. These can be placed after the "plot" function in your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
892
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
565
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top