1条回答 默认 最新
- joel_1993 2021-10-09 14:33关注
你好同学,代码仅供参考:
拉格朗日插值function [y,L] = Lagrange_Interp(X,Y,x) % X is a vector of independent variable values % Y is a vector of measurements of dependent variable values % x is the value of the independent variable to be interpolated % y is the interpolated value, evaluated at x % L is a vector of Lagrange polynomial values at x %all row vectors if size(X,2) == 1 X = X'; end l_X = length(X); X = -X; vector_product = zeros(l_X); for k = 1:l_X X_copy = X; X_copy(:,k) = []; den = prod(-X(k) + X_copy); vector_product(k,:) = (Y(k)/den) * my_prod(X_copy); end L = sum(vector_product,1); a = x.^ (length(L)-1:-1:0); y = sum(a.*L); end function [vector_product] = my_prod(X_copy) prod1 = [1 X_copy(1)]; for k = 2:length(X_copy) prod2 = [1 X_copy(k)]; prod_result = prod2; for m = 1:length(prod1)-1 prod_result = cat(2,prod_result,(prod1(m+1)*prod2)); end prod1 = my_sum(prod_result); end vector_product = prod1; end function [total] = my_sum(prod1) m = 1; total = zeros(1,(length(prod1)-2)/2); for k = 2:2:length(prod1)-1 total(m) = prod1(k) + prod1(k+1); m = m+1; end total = [prod1(1) total prod1(end)]; end
牛顿插值
function yp = Newtoninterp(x,y,xp) % Inputs % example data % x = [1 4 6 5]; % x vector % y = log(x); % y = f(x) vector % xp = 2 or xp = [2 3]; % x point(s) (scalar or vector) % Calculations n = numel(x); deg = n-1; % number of elements and degree polynomial np = numel(xp); % number of elements of xp % Example for degree = 3: convert to a for loop % y1 = diff(y)./(x(1+1:end)-x(1:end-1)) % y2 = diff(y1)./(x(1+2:end)-x(1:end-2)) % y3 = diff(y2)./(x(1+3:end)-x(1:end-3)) % interpolate yp = zeros(size(xp)); % temporary yp for j = 1:np dy = diff(y); % initial y difference ypj = y(1); % initial y point for i = 1:deg yi = dy./(x(1+i:end)- x(1:end-i)); % ith divided differences dy = diff(yi); % get and update the yi difference ypj = ypj+yi(1)*prod(ones(1,i)*xp(j)-x(1:i)); % y = f(x point) point per j end yp(j) = ypj; % y = f(x point) point(s) (scalar or vector) end end
答题不易,有帮助望采纳【右上角】支持答主继续奋斗
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 如何通过代码传输视频到亚马逊平台
- ¥15 php查询mysql数据库并显示至下拉列表中
- ¥15 freertos下使用外部中断失效
- ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
- ¥15 devserver配置完 启动服务 无法访问static上的资源
- ¥15 解决websocket跟c#客户端通信
- ¥30 Python调用dll文件输出Nan重置dll状态
- ¥15 浮动div的高度控制问题。
- ¥66 换电脑后应用程序报错
- ¥50 array数据同步问题