MATLAB: How to graph a function with a parameter that changes with time.

graphMATLABparameterplotplottingtime

I'm using Matlab to plot a function that varies with time. So for example I want to plot a function y = (1/(t+1))*exp(-t*x^2) where t changes values with time. So for example at start it takes t=0 value and as time passes the value of t increases little by little. I can graph a simple y=exp(-x^2) using linspace () and plot() but idk how to plot with time as a variable. Please help.

Best Answer

It is straightforward to define and calculate the result of ‘y’ while varying both ‘t’ and ‘x’ at the same time, using matrix arguments to ‘y’.
Example —
y = @(t,x) (1./(t+1)).*exp(-t.*x.^2); % Create As Anonymous Function
t = linspace(0, 10, 25); % Define ‘Time’ Vector
x = linspace(-2, 2, 15); % Deffine ‘Position’ Vector
[T,X] = ndgrid(t,x); % Create Matrices For Both (Can Also Use ‘meshgrid’)
figure
surfc(T,X,y(T,X))
grid on
xlabel('t')
ylabel('x')
zlabel('y')
Experiment to get different results.
Related Question