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 commentaires

Adam Danz
Adam Danz le 14 Avr 2020
Please provide the entire error message and point to the line producing the error.
Hong Le
Hong Le le 14 Avr 2020
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 le 14 Avr 2020
Here is the picture

Connectez-vous pour commenter.

Réponses (1)

James Tursa
James Tursa le 14 Avr 2020
Modifié(e) : James Tursa le 14 Avr 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 .*

2 commentaires

Hong Le
Hong Le le 14 Avr 2020
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 le 14 Avr 2020
By the way this is the question that I tried to solve

Connectez-vous pour commenter.

Catégories

Question posée :

le 14 Avr 2020

Commenté :

le 14 Avr 2020

Community Treasure Hunt

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

Start Hunting!

Translated by