Matrix dimensions must agree.

조회 수: 1 (최근 30일)
Nevin Pang
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');
title('Quadratic Spline')
  댓글 수: 1
Walter Roberson
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 ?

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
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
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)
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 2019년 3월 19일
If you have the symbolic toolbox you could use piecewise()

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by