Matrix dimensions must agree.

1 view (last 30 days)
Nevin Pang
Nevin Pang on 18 Mar 2019
Commented: Walter Roberson on 19 Mar 2019
Hi, i still new to matlab.
I've got an error that says "Matrix dimensions must agree."
The error is a line 4, "y = (0.943*(x-9).^2)-(24.99*t+6113.91); "
what do i need to do to correct the error?
x = 9:0.1:15;
N = length(x)-1;
y = (0.943*(x-9).^2)-(24.99*t+6113.91);
%Spline Equations
%The equations are substituted into the code.
%x1 (-(1.37)*t.^2)+6000; 0<=t<=9
%x2 (0.943*(x-9).^2)-(24.99*t+6113.91); 9<=t<=15
%x3 (-(0.6054)*(x-15).^2)-((13.34*t)+5975.1); 15<=t<=39
%x4 (-(6.1557)*(x-39).^2)-((960.74*t)+7254.97); 39<=t<=49
%x5 (5.622*(x-49).^2)-((183.8*t)+12673.95); 49<=t<=57
%x6 (2.4008*(x-57).^2)-((93.89*t)+7903.9); 57<=t<=69
%x7 (-(12.91)*(x-69).^2)-((36.27*t)+4273.63); 69<=t<=74
%x8 (3.091*(x-74).^2)-((92.92*t)+8324.598); 74<=t<=81
%x9 (0.2117*(x-81).^2)-((49.64*t)+4969.84); 81<=t<=96
%x10 (-(5.0625)*(x-96).^2)-((43.28*t)+4407.744); 96<=t<=100
% The unknowns are 3*N with ao=0 "Linear Spline"
% Filling Matrix from point matching
V = [0;zeros(2*N,1);zeros(N-1,1)];
Z = zeros(length(V),length(V));
j=1;
f=1;
for i=2:2:2*N
Z(i,f:f+2) = [x(j)^2 x(j) 1];
V(i) = y(j);
j = j+1;
Z(i+1,f:f+2) = [x(j)^2 x(j) 1];
V(i+1) = y(j);
f = f+3;
end
% Filling Matrix from smoothing condition
j=1;
l=2;
for i=2*N+2:3*N
Z(i,j:j+1) = [2*x(l) 1];
Z(i,j+3:j+4) = [-2*x(l) -1];
j = j+3;
l = l+1;
end
% Adjusting the value of a1 to be zero "Linear Spline"
Z(1,1)=1;
% Inverting and obtaining the coeffiecients, Plotting
Coeff = Z\V;
j=1;
hold on;
for i=1:N
curve=@(l) Coeff(j)*l.^2+Coeff(j+1)*l+Coeff(j+2);
ezplot(curve,[x(i),x(i+1)]);
hndl=get(gca,'Children');
set(hndl,'LineWidth',2);
hold on
j=j+3;
end
scatter(x,y,50,'r','filled')
grid on;
xlim([min(x)-2 max(x)+2]);
ylim([min(y)-2 max(y)+2]);
xlabel('x');
ylabel('y');
title('Quadratic Spline')
  1 Comment
Walter Roberson
Walter Roberson on 18 Mar 2019
We do not know the definition of t, but whatever it is is not the same size as x which is 1 x 61.
Are you trying to create a matrix of results, one output for each (x(J),t(K)) pair ?

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 18 Mar 2019
Look at line 4.
y = (0.943*(x-9).^2)-(24.99*t+6113.91);
We know what x is, that is, a vector.
x = 9:0.1:15;
But what is t? Is it possible that t is something else, already defined by you?
Now go back and read the error message. Then read the line of code you wrote.
  3 Comments
Nevin Pang
Nevin Pang on 19 Mar 2019
after running the code, i'd like to produce a graph with multiple results from my code but im unsure how to use the hold on function (ie; where to put within the code)
(mulitple results as in, i'm using the limits from 0-9, 9-15 etc as listed in the above code as t and the equations x1 thru 10.
figure (1)
title('Quadratic Spline')
xlim([min(t)-2 max(t)+2]);
ylim([min(y)-2 max(y)+2]);
xlabel('x');
ylabel('y');
grid on
grid minor
hold on
i placed a hold on at the end but i am not able to collate the results on one graph.
any help would be appreciated!
thank you!!
Walter Roberson
Walter Roberson on 19 Mar 2019
If you have the symbolic toolbox you could use piecewise()

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!