How to plot a two variable function with exponentials and cosine.

1 Ansicht (letzte 30 Tage)
EDIT: Sorry if i may seem clueless but my teacher literally uploaded this equation with a scenario of it being the powerloss equation for a laser beam, then he said plot the signal using matlab.
Hi I'm trying to plot a two variable function with exponentials and cosine. I am really rusty with matlab but I tried just about everything and I keep getting unspecific errors. I will atach my my teacher wanted us to plot.
Here is my code:
E(x,t)=150*exp(-0.03*x)*cos(3*10.^(-15)*(t)-10.^(7)*(x));
plot(E);

Akzeptierte Antwort

Star Strider
Star Strider am 4 Sep. 2020
I am not certain what you want.
Try tthis:
x = linspace(-pi, pi, 60); % Use Your Own Limits
t = linspace(0, 60, 50); % Use Your Own Limits
[X,T] = ndgrid(x,t);
E = @(x,t) 150*exp(-0.03*x) .* cos(3E-15.*t - 1E7.*x);
figure
plot(x,E(X,T))
grid
figure
plot(t,E(X,T))
grid
figure
surfc(X,T,E(X,T))
grid on
.
  2 Kommentare
Seleste Mendoza
Seleste Mendoza am 4 Sep. 2020
I am honestly not sure what my teacher wants... he was very vauge about what he wanted. He just dropped the equation and said to plot it on matlab. Nut thank you for your help I will try this

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

David Hill
David Hill am 4 Sep. 2020
Bearbeitet: David Hill am 4 Sep. 2020
It is important to know what the ranges are for x and t. I guessed. You must do a plot3 or surf or some other 3d type of plot.
[x,t]=meshgrid(1e-8:1e-9:1e-7,1e14:1e13:1e15);
E=@(x,t)150*exp(-0.03*x).*cos(3*10^(-15)*(t)-10^(7)*(x));
surf(x,t,E(x,t));
  2 Kommentare
David Hill
David Hill am 4 Sep. 2020
Bearbeitet: David Hill am 4 Sep. 2020
Provides more data with no edge color.
[x,t]=meshgrid(1e-9:1e-9:1e-6,1e13:1e12:1e16);
E=@(x,t)150*exp(-0.03*x).*cos(3*10^(-15)*(t)-10^(7)*(x));
a=surf(x,t,E(x,t));
a.EdgeColor = 'none';
Seleste Mendoza
Seleste Mendoza am 4 Sep. 2020
This makes alot of sense. Thank you. I will try this

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 4 Sep. 2020
You need to decide whether you are working symbolically or numerically. Your first line is not going to work unless you have defined
syms x t
in which case you are defining a symbolic function named E that expects two parameters. But plot() cannot be used with Symbolic functions: you would need fsurf()
If you are working numerically then you need to vectorize your code such as is shown by Dave Hill.
However where Star Strider and Dave showed creating numeric meshes, you could instead pass the function handle to fsurf along with the plot ranges and let it create values as needed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by