Azzera filtri
Azzera filtri

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

4 visualizzazioni (ultimi 30 giorni)
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;

Risposta accettata

Voss
Voss il 29 Gen 2024
Modificato: Voss il 29 Gen 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

Più risposte (0)

Tag

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by