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 ExchangeFit 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