matlab invalid expression error
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kathleen
el 1 de Oct. de 2023
Respondida: Image Analyst
el 1 de Oct. de 2023
Hi,
I am getting a lot of errors in my code, starting with line 24. Can someone please help me? Thanks!
clear, clc, close all
%%
set(0,'DefaultAxesLineWidth',2,'DefaultLineLineWidth',3)
set(0,'DefaultAxesFontSize',12,'DefaultAxesFontWeight','bold')
%% Generate the data set
% Time values (in seconds)
t = [1 10 10];
% Observed data (in meters)
y = m_true(1) + m_true(2)*t - m_true(3)*t.^2/2 + sigma*randn(10, 1);
% Standard deviation of the errors
sigma = 8 * ones(size(t));
% True model parameters
m_true = [10 100 9.8]; % [m1; m2; m3]
G = [ones(size(t)); t; -0.5 * t.^2];
W = diag(1./(sigma.^2));
m_ls = [G W G'] \ [G W y'];
Cov_m_ls = inv (G W G');
% Degrees of freedom
v = length(t) - length(m_ls);
% Chi-square statistic
chi_square = sum(((y - G' * m_ls) ./ sigma).^2);
p_value = 1 - chi2cdf(chi_square, v); % p-value
% Significance level
% (95% confidence interval)
alpha = 0.05;
% Z-score for alpha
z_alpha = norminv(1 - alpha/2);
confidence_intervals =
[m_ls - z_alpha sqrt(diag(Cov_m_ls)),
m_ls + z_alpha sqrt(diag(Cov_m_ls))];
fprintf('Parameter Estimates (m_ls):\n');
disp(m_ls);\
fprintf('Confidence Intervals (95%%):\n');
disp(confidence_intervals);
fprintf('Chi-Square: %.2f\n', chi_square);
fprintf('Degrees of Freedom: %d\n', v);
fprintf('p-value: %.4f\n', p_value);
t_fit = linspace(min(t), max(t), 100);
% Generate points for the fitted curve
y_fit = m_ls(1) + m_ls(2) t_fit
- (1/2) m_ls(3) * t_fit.^2;
% Fitted model
figure;
plot(t, y, 'o', 'DisplayName', 'Observed Data');
hold on;
plot(t_fit, y_fit, 'r-',
'DisplayName', 'Fitted Model');
xlabel('Time (s)');
ylabel('Distance (m)');
legend('Location', 'Best');
title('Ballistics Regression');
grid on;
0 comentarios
Respuesta aceptada
Image Analyst
el 1 de Oct. de 2023
Tons of errors. I got tired of fixing them all, but I got this far in your script. You can continue to fix them one at a time as you go down the script:
%%
set(0,'DefaultAxesLineWidth',2,'DefaultLineLineWidth',3)
set(0,'DefaultAxesFontSize',12,'DefaultAxesFontWeight','bold')
%% Generate the data set
% Time values (in seconds)
t = [1 10 10];
numPoints = numel(t);
% True model parameters
m_true = [10 100 9.8]; % [m1; m2; m3]
% Standard deviation of the errors
sigma = 8 * ones(numPoints);
% Observed data (in meters)
y = m_true(1) + m_true(2)*t - m_true(3)*t.^2/2 + sigma .* randn(1, numPoints);
G = [ones(1, numPoints); t; -0.5 * t.^2]
W = diag(1./(sigma.^2))
0 comentarios
Más respuestas (1)
Sam Chak
el 1 de Oct. de 2023
Hi @Kathleen
Fixed "invalid expression errors" are marked by these arrows ''<--". However, the variable m_true is not provided in the code.
clear, clc, close all
%%
set(0,'DefaultAxesLineWidth',2,'DefaultLineLineWidth',3)
set(0,'DefaultAxesFontSize',12,'DefaultAxesFontWeight','bold')
%% Generate the data set
% Time values (in seconds)
t = [1 10 10];
% Observed data (in meters)
y = m_true(1) + m_true(2)*t - m_true(3)*t.^2/2 + sigma*randn(10, 1);
% Standard deviation of the errors
sigma = 8 * ones(size(t));
% True model parameters
m_true = [10 100 9.8]; % [m1; m2; m3]
G = [ones(size(t)); t; -0.5 * t.^2];
W = diag(1./(sigma.^2));
m_ls = [G W G'] \ [G W y'];
Cov_m_ls = inv([G W G']); % <--
% Degrees of freedom
v = length(t) - length(m_ls);
% Chi-square statistic
chi_square = sum(((y - G' * m_ls) ./ sigma).^2);
p_value = 1 - chi2cdf(chi_square, v); % p-value
% Significance level
% (95% confidence interval)
alpha = 0.05;
% Z-score for alpha
z_alpha = norminv(1 - alpha/2);
confidence_intervals = [m_ls - z_alpha sqrt(diag(Cov_m_ls)), m_ls + z_alpha sqrt(diag(Cov_m_ls))]; % <--
fprintf('Parameter Estimates (m_ls):\n');
disp(m_ls); % <--
fprintf('Confidence Intervals (95%%):\n');
disp(confidence_intervals);
fprintf('Chi-Square: %.2f\n', chi_square);
fprintf('Degrees of Freedom: %d\n', v);
fprintf('p-value: %.4f\n', p_value);
t_fit = linspace(min(t), max(t), 100);
% Generate points for the fitted curve
y_fit = m_ls(1) + m_ls(2)*t_fit - (1/2)*m_ls(3)*t_fit.^2; % <--
% Fitted model
figure;
plot(t, y, 'o', 'DisplayName', 'Observed Data');
hold on;
plot(t_fit, y_fit, 'r-', 'DisplayName', 'Fitted Model'); % <--
xlabel('Time (s)');
ylabel('Distance (m)');
legend('Location', 'Best');
title('Ballistics Regression');
grid on;
1 comentario
Image Analyst
el 1 de Oct. de 2023
It's also weird that the second and third time value are the very same time of 10.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!