[MATLAB] Fractal interpolation

every blog every motto: The shortest answer is doing.

0. Preface

This section briefly introduces the fractal interpolation code based on Matlab. There is relatively little information on this aspect on the Internet, so I will briefly organize it here.

1. Text

1.1 Two-dimensional (x, y) fractal interpolation

1.1.1 Results display

Create a new txt file for the original point data, delete the suffix after saving it, and put it in the ./data folder
Insert picture description here

Insert picture description here
Iteration 1 time, effect 3
Insert picture description here
iterations effect
Insert picture description here

1.1.2 Code section

% This is a fline.m program (fractal line interpolation)
clear
clc

% 前期设置
% file_name = input('输入数据文件名:','s');
% rate = input('输入纵向压缩比:');
% iteration_num = input('输入迭代次数(<4):');
file_name = 'num';
rate = 0.3;
iteration_num = 3;
t = num2str(iteration_num); % 迭代次数数值转字符串

% 输入、输出路径
input_path = ['./data/' file_name]; % 输入数据存放位置
output_path = [input_path '_output_' t]; % 输出数据存放位置
hold off

% 加载数据
eval(['load ' input_path]);
eval(['data=' file_name ';']); % 将数据存放在data变量中
eval(['clear ' file_name]); % 删除文件名变量的值

x = data(1,:); % x 
y = data(2,:); % y

[t,n] = size(x);

denominator = x(n) - x(1); % 公式分母
point_section = n-1; % 插值点区间
plot(x,y,'--ok');
hold on

% 公式计算
for k=1: iteration_num % 迭代iteration_num次
    disp(['迭代次数:' ,num2str(k)])
  
    for i=1:point_section
        a(i) = (x(i+1)-x(i))/denominator;
        e(i) = (x(n)*x(i)-x(1)*x(i+1))/denominator;
        c(i) = (y(i+1)-y(i)-rate*(y(n)-y(1)))/denominator;
        f(i) = ((x(n)*y(i)-x(1)*y(i+1))-rate*(x(n)*y(1)-x(1)*y(n)))/denominator;
    end
    
    for i=1:point_section
        for j=1:n
            xx((i-1)*point_section+j) = a(i)*x(j)+e(i);
            yy((i-1)*point_section+j) = c(i)*x(j) + rate*y(j)+f(i);
        end
    end
    disp(['------第' num2str(k)  '次迭代完成-------']);
    size(yy)
    % 迭代计算
    [t,n] = size(xx);
    point_section = n-1;
    % 不同的线性
    if k==1,line=''':k''';end
    if k>=2,line='''-k''';end
    eval(['plot(xx,yy, ' line ')']); % 画图
    x = xx;
    y = yy;
end

% 保存最后一次数据
save_con_str = ['fwd=fopen(' '''' output_path '''' ',' '''' 'w' '''' ')']; 
eval([save_con_str ';']);

for i=1:n
    fprintf(fwd,'%8.4f',xx(i));
end
fprintf(fwd,'\n') ;
for i=1:n
    fprintf(fwd,'%8.4f',yy(i));
end
fclose(fwd);



1.2 Three-dimensional (x, y, z) fractal interpolation

1.2.1 Results display

Original data, put it in the current folder (./data/), create a new txt, delete the suffix after saving the data
Insert picture description here
Result 1:
Insert picture description here
Result 2:
Insert picture description here

1.2.2 Code

Complete code for 2D and 3D fractal interpolation:
https://download.csdn.net/download/weixin_39190382/12674351

Guess you like

Origin blog.csdn.net/weixin_39190382/article/details/107695224