# Matrix dimensions must agree.

2 ビュー (過去 30 日間)
Nevin Pang 2019 年 3 月 18 日
コメント済み: Walter Roberson 2019 年 3 月 19 日
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');

#### 1 件のコメント

Walter Roberson 2019 年 3 月 18 日
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 ?

サインイン to comment.

### 採用された回答

John D'Errico 2019 年 3 月 18 日
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 件のコメント

Nevin Pang 2019 年 3 月 19 日
my error was writing the wrong alphabet t instead of x which the code uses.
Nevin Pang 2019 年 3 月 19 日
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)
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 2019 年 3 月 19 日
If you have the symbolic toolbox you could use piecewise()

サインイン to comment.

### その他の回答 (0 件)

サインイン してこの質問に回答します。

Translated by