本文共 1143 字,大约阅读时间需要 3 分钟。
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
问题:在使用 MATLAB 去做 Neville's Algorithm 的时候,因为要涉及到参数(符号变量)的保存,所以要把运算结果储存到一个数组当中。代码如下:
% num of interpolation points and curve function
n = 10;
[T,Qx,Qy] = Value(n);
syms t;
% Neville's Process
for j=1:n
for i=1:n-j+1
Qx(i) = (T(i+j)-t)/(T(i+j)-T(i))*Qx(i)+(t-T(i))/(T(i+j)-T(i))*Qx(i+1);
Qy(i) = (T(i+j)-t)/(T(i+j)-T(i))*Qy(i)+(t-T(i))/(T(i+j)-T(i))*Qy(i+1);
end
end
其中 Value 为调用的函数,是参数方程求解 -x,-y 坐标,具体如下:
% Function: using parametric function calculate the -x and -y value
function [t,Px,Py] = Value(n)
t = -1:(2/n):1; % sampling
for i = 1:n+1
Px(i,1) = (25*t(i)^2+2)./(25*t(i)^2+1).*(sin(pi*t(i)/2));
Py(i,1) = (25*t(i)^2+2)./(25*t(i)^2+1).*(cos(pi*t(i)/2));
end
end
运行之后,提示错误:
The following error occurred converting from sym to double:
DOUBLE cannot convert the input expression into a double array.
Error in NevillesAlgorithm (line 19)
Qx(i) = (T(i+j)-t)/(T(i+j)-T(i))*Qx(i)+(t-T(i))/(T(i+j)-T(i))*Qx(i+1);
我看了数据类型,确实 Qx 和 Qy 两个数组都是11*1的double,但是我这里想要通过迭代,储存含有参数 t 的表达式。请问有解么?
ps.
我自己做了一些实验发先如果代码如下,是可以储存带有参数的矩阵的。这又是何解?
>> syms a
>> A = [1,2,3;4,5,6];
>> A = A+a;
>> A
A =
[ a + 1, a + 2, a + 3]
[ a + 4, a + 5, a + 6]
转载地址:http://odevl.baihongyu.com/