Matrix dimensions must agree error

I tried to plot the fitting plot but I ran to this error. Can anyone help me with this please?
clear; close all; clc;
load t2pb2data.mat;
g = g(:);
t = t(:);
plot(t,g,'r*');
xlabel('t [sec]');
ylabel('g');
%Calculate p1 and p2
A = [log(t), ones(size(t))];
z = inv(A'*A)*A'*g;
c1 = z(1);
c2 = z(2);
p1 = c1/(-3);
p2 = -log(c2/sin(0.3*t));
%Display value of p1 and p2 in formarted
fprintf('p1 = %.4f. \n', p1);
fprintf('p2 = %.4f. \n', p2);
%Calculate R2
gh = p1*log(1./(t.^3))+exp(-p2)*sin(0.3*t);
gb = mean(g);
R2 = 1 - sum((g -gh).^2)/sum((g-gb).^2);
%Display value of R2 in formarted
fprintf('R2 = %.5f. \n', R2);
%Plot the fitting curve against the experimental data
hold on
tf = linspace(min(t), max(t),100);
gf = p1.*log(1./(tf.^3))+exp(-p2).*sin(0.3.*tf);
plot(tf,gf,'b');
% Add legend
legend('Experimental Data', 'Fitting Curve')

댓글 수: 3

Adam Danz
Adam Danz 2020년 4월 14일
Please provide the entire error message and point to the line producing the error.
Hong Le
Hong Le 2020년 4월 14일
Matrix dimensions must agree.
Error in Assignment10 (line 32)
gf = p1.*log(1./(tf.^3))+exp(-p2).*sin(0.3.*tf);
Hong Le
Hong Le 2020년 4월 14일
Here is the picture

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

답변 (1개)

James Tursa
James Tursa 2020년 4월 14일
편집: James Tursa 2020년 4월 14일

0 개 추천

t and g look like they are vectors, so use element-wise operators (with the . dot) when dealing with equations involving them. E.g., this
p2 = -log(c2/sin(0.3*t));
should probably be this instead
p2 = -log( c2 ./ sin(0.3*t) ); % changed / to ./
and other changes such as
gh = p1*log(1./(t.^3)) + exp(-p2) .* sin(0.3*t); % changed * to .*

댓글 수: 2

Hong Le
Hong Le 2020년 4월 14일
Thank you a lot for your help. After making the changes I be able to see the result. But the fitting plot is kinda weird.
Hong Le
Hong Le 2020년 4월 14일
By the way this is the question that I tried to solve

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

카테고리

도움말 센터File Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

질문:

2020년 4월 14일

댓글:

2020년 4월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by