フィルターのクリア

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

4 ビュー (過去 30 日間)
Kenzie
Kenzie 2024 年 1 月 29 日
コメント済み: Voss 2024 年 1 月 29 日
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;

採用された回答

Voss
Voss 2024 年 1 月 29 日
編集済み: Voss 2024 年 1 月 29 日
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 件のコメント
Kenzie
Kenzie 2024 年 1 月 29 日
Thank you!
Voss
Voss 2024 年 1 月 29 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by