필터 지우기
필터 지우기

matlab invalid expression error

조회 수: 5 (최근 30일)
Kathleen
Kathleen 2023년 10월 1일
답변: Image Analyst 2023년 10월 1일
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;

채택된 답변

Image Analyst
Image Analyst 2023년 10월 1일
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))

추가 답변 (1개)

Sam Chak
Sam Chak 2023년 10월 1일
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);
Unrecognized function or variable 'm_true'.
% 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
Image Analyst
Image Analyst 2023년 10월 1일
It's also weird that the second and third time value are the very same time of 10.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Direction of Arrival Estimation에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by