making a residual graph with a function as a base line

Hi,
I have a project i'd like to make a rusidual graph for but I'd like it to be between my data and the matlab curve fit for the data to show how close they are to one another. This is my code for the plot of the curve and my data, I tried to add a stem command to add the residual but is seems the base line must be a scalar. is there a way I can change the base line to a function?
I's like the graph to look something like this picture https://upload.wikimedia.org/wikipedia/commons/1/17/MDKQ1.svg
my code is added to this post right here:
H=[2.5;3.7;4.5;5.3;6.5;7.5;8.5;10.5;12.5;14.5;17.5;20.5;23.5;26.5];
g=[8.38;10.08;11.34;12.183;14.1;13.46;14.5;16.55;18.06;19.08;21.617;23.253;25.414;26.988];
m=[8.067;9.983;10.66;11.983;13.02;13.52;14.35;15.74;17.6;18.7;20.983;22.383;25.014;25.957];
errg=[0.194,0.643,0.413,0.452,0.503,0.162,0.438,0.544,0.258,0.293,0.385,0.354,0.353,0.372];
errm=[0.512,0.291,0.12,0.376,0.16,0.402,0.112,0.338,0.297,0.447,0.241,0.186,0.641,0.424];
f=fit(H,g,'power1');
plot(f,H,g,'-o')
hold on
stem(H,(g-f(H)),'BaseValue',-y_shift)
hold off
set(gca,'Fontsize',12)
title('מרחק פגיעה כדור זכוכית כתלות בגובה המגלשה')
xlabel('גובה H')
ylabel('מרחק פגיעה')
legend('גרף התאמה','נתוני מדידות')
axis([0,max(H)+1,0,max(g)+1])
grid on
thank you so much!

 Accepted Answer

H=[2.5;3.7;4.5;5.3;6.5;7.5;8.5;10.5;12.5;14.5;17.5;20.5;23.5;26.5];
g=[8.38;10.08;11.34;12.183;14.1;13.46;14.5;16.55;18.06;19.08;21.617;23.253;25.414;26.988];
m=[8.067;9.983;10.66;11.983;13.02;13.52;14.35;15.74;17.6;18.7;20.983;22.383;25.014;25.957];
errg=[0.194,0.643,0.413,0.452,0.503,0.162,0.438,0.544,0.258,0.293,0.385,0.354,0.353,0.372];
errm=[0.512,0.291,0.12,0.376,0.16,0.402,0.112,0.338,0.297,0.447,0.241,0.186,0.641,0.424];
f=fit(H,g,'power1');
% make a green line for the residual, using NaNs to break it into segments:
% its xdata is [H(1) H(1) NaN H(2) H(2) NaN ...]
% its ydata is [g(1) f(H(1)) NaN g(H(2)) H(2) NaN ...]
residual_xdata = [H H NaN(numel(H),1)].';
residual_ydata = [g f(H) NaN(numel(H),1)].';
h_residual = plot(residual_xdata(:),residual_ydata(:),'g','LineWidth',2);
hold on
% plot the other two using "regular" plot() instead of curvefit plot() to
% have more control over the line properties:
h_f = plot(H,f(H),'-r','LineWidth',2);
h_g = plot(H,g,'bo','MarkerFaceColor','b','MarkerSize',4);
hold off
set(gca,'Fontsize',12)
title('מרחק פגיעה כדור זכוכית כתלות בגובה המגלשה')
xlabel('גובה H')
ylabel('מרחק פגיעה')
% specify the line handles and location in legend():
legend([h_g h_f h_residual],{'גרף התאמה','נתוני מדידות','residual'},'Location','southeast')
% axis([0,max(H)+1,0,max(g)+1])
% for demonstration purposes, set the xlim and ylim to see it better:
axis([5 10 10 15])
grid on

4 Comments

thank you so much! it works perfectly can you please explain more about this line:
residual_xdata = [H H NaN(numel(H),1)].';
and what does it define?
in any case thank you so much!!
You're welcome!
Take a look at what that line and the the next one do:
H=[2.5;3.7;4.5;5.3;6.5;7.5;8.5;10.5;12.5;14.5;17.5;20.5;23.5;26.5];
g=[8.38;10.08;11.34;12.183;14.1;13.46;14.5;16.55;18.06;19.08;21.617;23.253;25.414;26.988];
f=fit(H,g,'power1');
% remove the semicolons at the end of each line, to show the results in the command window
residual_xdata = [H H NaN(numel(H),1)].'
residual_xdata = 3×14
2.5000 3.7000 4.5000 5.3000 6.5000 7.5000 8.5000 10.5000 12.5000 14.5000 17.5000 20.5000 23.5000 26.5000 2.5000 3.7000 4.5000 5.3000 6.5000 7.5000 8.5000 10.5000 12.5000 14.5000 17.5000 20.5000 23.5000 26.5000 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
residual_ydata = [g f(H) NaN(numel(H),1)].'
residual_ydata = 3×14
8.3800 10.0800 11.3400 12.1830 14.1000 13.4600 14.5000 16.5500 18.0600 19.0800 21.6170 23.2530 25.4140 26.9880 8.1824 9.9519 10.9739 11.9083 13.1861 14.1629 15.0764 16.7543 18.2785 19.6848 21.6230 23.4009 25.0526 26.6018 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
These are matrices of x- and y- coordinates of the points of the green "residual" line. The x's come in pairs, i.e., each segment is vertical, and the y's are g and f(H), the two curves to plot between. (They are matrices because that makes it convenient to append a column of NaNs - the NaNs essentially introduce a "break" in the line after each pair of points.)
Then they are converted into column vectors (using (:)), for plotting, on the next line:
h_residual = plot(residual_xdata(:),residual_ydata(:),'g','LineWidth',2);
Have a look at residual_xdata(:) and residual_ydata(:):
residual_xdata(:)
ans = 42×1
2.5000 2.5000 NaN 3.7000 3.7000 NaN 4.5000 4.5000 NaN 5.3000
residual_ydata(:)
ans = 42×1
8.3800 8.1824 NaN 10.0800 9.9519 NaN 11.3400 10.9739 NaN 12.1830
Now the x- and y-coordinates are in the proper order. NaNs serve to break up the plotted line into segments (in this case, vertical segments of two points apiece). That is, the first segment goes from (2.5, 8.38) to (2.5, 8.1824), the second segment goes from (3.7, 10.08) to (3.7, 9.9519), and so on.
again thank you so much this as been so helpful!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!