Effacer les filtres
Effacer les filtres

Why am I getting a 'parse error' at t and how do I fix it?

4 vues (au cours des 30 derniers jours)
Kenzie
Kenzie le 29 Jan 2024
Commenté : Voss le 29 Jan 2024
I am trying to write a program that returns the gaussian distribution given input arguments of a vector times t and a value of RMS deviation tau.
This is my code, but it keeps saying there is a parse error at line 14: "Parse error at t: usage might be invalid MATLAB syntax"
% Write a program that returns the Gaussian distribution given input
% arguments of a vector of times t and a value of a root mean squared (RMS)
% deviation tau
function gaussian_distribution = generate_gaussian(t, tau)
% t: Vector of times
% tau: RMS deviation
% Calculate the Gaussian distribution
gaussian_distribution = exp(-t.^2 / (2 * tau^2)) / (tau * sqrt(2 * pi));
end
% Example usage and plot
t = linspace(-5, 5, 1000); % Example time vector
This statement is not inside any function.
(It follows the END that terminates the definition of the function "generate_gaussian".)
tau = 1.5; % Example RMS deviation
% Generate Gaussian distribution
gaussian_dist = generate_gaussian(t, tau);
% Plot the Gaussian distribution
figure;
plot(t, gaussian_dist, 'LineWidth', 2);
title('Gaussian Distribution');
xlabel('Time');
ylabel('Amplitude');
grid on;

Réponse acceptée

Voss
Voss le 29 Jan 2024
Modifié(e) : Voss le 29 Jan 2024
Function definitions in a script must be at the end of the script, like this:
% Example usage and plot
t = linspace(-5, 5, 1000); % Example time vector
tau = 1.5; % Example RMS deviation
% Generate Gaussian distribution
gaussian_dist = generate_gaussian(t, tau);
% Plot the Gaussian distribution
figure;
plot(t, gaussian_dist, 'LineWidth', 2);
title('Gaussian Distribution');
xlabel('Time');
ylabel('Amplitude');
grid on;
% Write a program that returns the Gaussian distribution given input
% arguments of a vector of times t and a value of a root mean squared (RMS)
% deviation tau
function gaussian_distribution = generate_gaussian(t, tau)
% t: Vector of times
% tau: RMS deviation
% Calculate the Gaussian distribution
gaussian_distribution = exp(-t.^2 / (2 * tau^2)) / (tau * sqrt(2 * pi));
end
  2 commentaires
Kenzie
Kenzie le 29 Jan 2024
Thank you!
Voss
Voss le 29 Jan 2024
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by