# plotting 3 figures in a single figure when they are in loop

1 view (last 30 days)
sanket neharkar on 24 May 2022
Answered: Mathieu NOE on 24 May 2022
i had plotted a least square fit plot and i have calculate the error , i have fitted a curve withe the 5,7,9 points approximations, now i have to draw the graphs of the error for all the approximations in a single figure for comparison,
function phi = curvefit3(x,y)
clear all;
close all;
clc;
x_imp = data(:,1); %%reading rows and first column
y_imp = data(:,2); %%reading rows and second column
[rows, ~] = size(data);
for i = 5:2:9
window = i;
fig(i) = figure('name', sprintf('%d points curve fit', i),'NumberTitle','off');
for i = 1: 1 : rows -window %%loop to read i+1 to i+5 rows
x = x_imp(i:i+window-1,1);
y = y_imp(i:i+window-1,1);
x0 = x(1);
var1 = x - x0;
var2 = var1.*var1;
%% construction of the least-squares quadratic fit to the data
%% we use the equation y =a0 + a1t + a2t^2 %%t = (x-x0)
%% numbers a0,a1 and a2 are the unknowns
x_mat = [ones(window,1) var1 var2];
amat = x_mat'*x_mat;
bmat = x_mat'*y;
phi = inv(amat)*bmat;
a0 = phi(1);
a1 = phi(2);
a2 = phi(3);
error = y-(x_mat*phi);
marker = floor(window/2) + 1;
if (i == 1)
err(1:marker,1) =error(1:marker,1);
else
err(i+marker-1) =error(marker);
end
pos_n = (a0+a1*var1+a2*var2);
markar = floor(window/2) + 1;
if (i == 1)
pos(1:markar,1) = pos_n(1:markar,1);
else
pos(i+markar-1) = pos_n(markar);
% plot(pos,'-ob');
end %if (i == 1)
velo = diff(pos);
acc_n = diff(diff(pos));
end %for i = 1: 1 : rows-window
n= length(pos);
tim_pos = x_imp(1:n);
tim_velo = x_imp(1:n-1);
tim_acc_n = x_imp(1:n-2);
y_raw = y_imp(1:n);
plot(tim_pos,y_raw,'or',tim_pos,pos','-g',tim_velo,velo','--k',tim_acc_n,acc_n','*b'); grid;
legend(' raw data', 'Curve Fit',' velocity', 'acceleration');
xlabel(' Time(sec) ');
ylabel(' Function ');
fig(i) = figure('name', sprintf('%d points error', i),'NumberTitle','off');
plot(tim_pos,err,'--k');grid;
legend('error');
end %for i = 5:2:9
return;
what should i do in this so that whatever the error graph i have plotted will come in a single figure ,with legend as 5pointerror,and so on

Mathieu NOE on 24 May 2022
hello
see my suggestion below (based on dummy data as you don't supply any file)
notice that to compute the velocity and acceleration you can use the function gradient instead of diff , which has the advantage to keep all data length equal . Also do not forget to use the dx spacing with diff or gradient otherwise your derivatives are wrong;
% function phi = curvefit3(x,y)
clear all;
close all;
clc;
% x_imp = data(:,1); %%reading rows and first column
% y_imp = data(:,2); %%reading rows and second column
%% dummy data
x_imp = (0:0.1:10)';
y_imp = 0.5 +1.2*x_imp + 0.54*x_imp.^2 + 0.02*randn(size(x_imp));
data = [x_imp y_imp];
[rows, ~] = size(data);
dx = mean(diff(x_imp));
points_curve_fit = 5:2:9;
ci = 0;
for nwindow = points_curve_fit
ci = ci+1;
for k = 1: 1 : rows -nwindow %%loop to read i+1 to i+5 rows
x = x_imp(k:k+nwindow-1,1);
y = y_imp(k:k+nwindow-1,1);
x0 = x(1);
var1 = x - x0;
var2 = var1.*var1;
%% construction of the least-squares quadratic fit to the data
%% we use the equation y =a0 + a1t + a2t^2 %%t = (x-x0)
%% numbers a0,a1 and a2 are the unknowns
x_mat = [ones(nwindow,1) var1 var2];
amat = x_mat'*x_mat;
bmat = x_mat'*y;
phi = inv(amat)*bmat;
a0 = phi(1);
a1 = phi(2);
a2 = phi(3);
error = y-(x_mat*phi);
marker = floor(nwindow/2) + 1;
if (k == 1)
err(1:marker,1) =error(1:marker,1);
else
err(k+marker-1) =error(marker);
end
pos_n = (a0+a1*var1+a2*var2);
markar = floor(nwindow/2) + 1;
if (k == 1)
pos(1:markar,1) = pos_n(1:markar,1);
else
pos(k+markar-1) = pos_n(markar);
% plot(pos,'-ob');
end %if (i == 1)
end
% store iteration results into 2D array (1 iteration = 1 column)
err_all(:,ci) = err; % to plot all outputs
pos_all(:,ci) = pos; % to plot all outputs
velo_all(:,ci) = velo; % to plot all outputs
acc_n_all(:,ci) = acc_n; % to plot all outputs
str_pos{ci} = ['Pos Curve Fit' num2str(points_curve_fit(ci)) ' points'];
str_velo{ci} = ['Velo Curve Fit' num2str(points_curve_fit(ci)) ' points'];
str_acc{ci} = ['Accel Curve Fit' num2str(points_curve_fit(ci)) ' points'];
str_err{ci} = ['error ' num2str(points_curve_fit(ci)) ' points'];
end
n= length(pos);
tim_pos = x_imp(1:n);
y_raw = y_imp(1:n);
fig1 = figure('name', sprintf('%d points curve fit ', points_curve_fit),'NumberTitle','off');
plot(tim_pos,y_raw,'or',tim_pos,pos_all,'-',tim_pos,velo_all,'--',tim_pos,acc_n_all,'*'); grid on;
legend([{' raw data'} str_pos str_velo str_acc]);
xlabel(' Time(sec) ');
ylabel(' Function ');
fig2 = figure('name', sprintf('%d points error ', points_curve_fit),'NumberTitle','off');
plot(tim_pos,err_all,'--');grid on;
legend(str_err);

### Categories

Find more on Surface and Mesh Plots in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by