Matrix dimensions must agree error
Show older comments
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')
Answers (1)
James Tursa
on 14 Apr 2020
Edited: James Tursa
on 14 Apr 2020
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 .*
Categories
Find more on Fit Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
