Unrecognized function or variable 'x'. Error (line 65)
2 次查看(过去 30 天)
显示 更早的评论
am computing and I get an error on line 65
x_v=[1 2 5 6 7 8 10 13 17]; % The corresponding x values
y_v = [3 3.7 3.9 4.2 5.7 6.6 7.7 6.7 4.5]; % The corresponding y values
dy_v= [1 NaN NaN -0.67]; % The slopes/clamps at the end
n=length(x_v) -1; % Find the n for size of the matrix
h_v = diff(x_v); % Find the h values (step size) from x values
%% Create Matrix A
for i=1:n+1 % Start row loop
for j=1:n+1 % Start column loop
if i==1&&j==1 % Condition for first row and first column
A(i,j) = 2*h_v(i); % First Entry
elseif i==n+1&&j==n+1 % Condition for last row and last column
A(i,j) = 2*h_v(i-1); % Last Entry
elseif j==i-1 % Condition for element jsut left of the diagonal
A(i,j) = h_v(i-1);
elseif j==i+1 % Condition for element jsut right of the diagonal
A(i,j) = h_v(i);
elseif i==j % Condition for element in diagonal
A(i,j) = 2*(h_v(i-1)+h_v(i));
else
A(i,j) = 0; % Condition for all other elements in matrix
end
end
end
%% Create Vector B
for i=1:n+1
if i==1 % Condition for first row
B(i) = 3*((1/h_v(i))*(y_v(i+1) - y_v(i)) - dy_v(i));
elseif i==n+1 % Condition for last row
B(i) = 3*(dy_v(i) - (1/h_v(i-1))*(y_v(i) - y_v(i-1)) );
else % Condition for all other elements
B(i) = 3*((1/h_v(i))*(y_v(i+1) - y_v(i)) - (1/h_v(i-1))*(y_v(i) - y_v(i-1)));
end
end
%% Find the Matrix C
c = (A^-1)*B'; % Find the product of inverse of A and B
for i=1:n % Loop for remaining constants (bs) and (ds)
b(i) = (1/h_v(i))*(y_v(i+1) - y_v(i)) - (h_v(i)/3)*(2*c(i) + c(i+1));
d(i) = (1/(3*h_v(i)))*(c(i+1) - c(i));
end
%% Find the splines
for i=1:n
S{i} = num2str(y_v(i),8) + num2str(b(i),8)*(x- num2str(x_v(i),8) ) + num2str(c(i),8)*(x- num2str(x_v(i),8)).^2 + num2str(d(i),8)*(x- num2str(x_v(i),8)).^3;
SS{i} =['$$' latex(S{i}) '$$'];
SS2{i} =[ latex(S{i}) ];
SSD{i} =[ latex(diff(S{i})) ];
end
2 个评论
the cyclist
2020-10-20
I don't think you posted 65 lines of code, so that's not very helpful reference. When I run the code you posted, I get an error on this line:
B(i) = 3*(dy_v(i) - (1/h_v(i-1))*(y_v(i) - y_v(i-1)) );
but not the error you report.
Assuming you are getting an error from this line:
S{i} = num2str(y_v(i),8) + num2str(b(i),8)*(x- num2str(x_v(i),8) ) + num2str(c(i),8)*(x- num2str(x_v(i),8)).^2 + num2str(d(i),8)*(x- num2str(x_v(i),8)).^3;
it seems as though you try to use the variable x, which has not been defined.
回答(1 个)
Walter Roberson
2020-10-21
num2str returns a character vector. You are trying to add and subtract character vectors, which MATLAB considers the same thing as creating a vector of double of the character codes needed to represent the characters, and attempting to add or subtract those. That is not going to work because the vectors are different lengths.
You look like perhaps you are trying to concatenate strings.You can use + to concatenate string objects but not character vectors. You would need to use string() around each of the num2str. But even if you did that, x minus a string object is not defined for any MATLAB object classes that I can think of.
If you are trying to do string concatenation please see sprintf() or compose()
Meanwhile you need to figure out what your x variable is.
Note also that since r2018b it has not been permitted to use diff() with a character vector. If you are attempting to take the derivative of a symbolic formula then build up the formula symbolically instead of by characters. I suspect that your x is intended to be a symbolic variable
syms x
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!