Trying to graph function giving me an incorrect scale

Hi, newbie here. I am trying to graph the following surface
f(x,t) = -> note its e^() * sin() at the end, not e^(() * sin())
in the range 0 <= t <= 2 , 0 <= x <= 1
with the following code:
clear
L=1;
nstep=25;
x=linspace(0,L,nstep);
t=linspace(0,2*L, nstep);
[X,T] = meshgrid(x,t)
exponent = -0.25*power((pi),2)*T;
f = (80/power(pi,2))*sin((pi)/2)*(exp(exponent))*(sin(pi*X))
surf(X,T,f)
I get the following graph:
This is way out of proportion. As you can see in the equation and in the given range, there is no way f(x,t) would have any values in the hundreds. It seems as if the surface looks right, but the range of f(x,t) values is way to big. Sorry if it is a dumb question, but am I multiplying something wrong? I just want to plot the graph of the equation i put above.

Answers (1)

"....but am I multiplying something wrong?"
Yes: you used matrix operations, whereas you need to use element-wise array operations:
To use MATLAB you need to learn the difference, otherwise your code will just produce nonsense and you won't know why.
Fixed code (I indicated the one location which must be an array operation):
>> x = linspace(0,1,25);
>> t = linspace(0,2,25);
>> [X,T] = meshgrid(x,t);
>> E = -0.25*power(pi,2)*T;
>> F = (80/power(pi,2))*sin(pi/2)*(exp(E)).*(sin(pi*X));
% ^^ must be array operation
surf(X,T,F)

Categories

Commented:

on 4 May 2020

Community Treasure Hunt

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

Start Hunting!