How to plot a two variable function with exponentials and cosine.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Seleste Mendoza
le 4 Sep 2020
Modifié(e) : David Hill
le 4 Sep 2020
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 commentaires
Réponse acceptée
Star Strider
le 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 commentaires
Plus de réponses (2)
David Hill
le 4 Sep 2020
Modifié(e) : David Hill
le 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 commentaires
David Hill
le 4 Sep 2020
Modifié(e) : David Hill
le 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
le 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.
Voir également
Catégories
En savoir plus sur Surface and Mesh Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!