making a residual graph with a function as a base line

1 view (last 30 days)
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

Voss
Voss on 29 Mar 2022
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

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!