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')
回答(1 个)
James Tursa
2020-4-14
编辑:James Tursa
2020-4-14
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 .*
类别
在 帮助中心 和 File Exchange 中查找有关 Fit Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

