How to plot a two variable function with exponentials and cosine.
2 views (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
Star Strider
on 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 Comments
More Answers (2)
David Hill
on 4 Sep 2020
Edited: David Hill
on 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 Comments
David Hill
on 4 Sep 2020
Edited: David Hill
on 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';
Walter Roberson
on 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.
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!