Plotting the resual difference between two plots, example picture in the thread.

2 次查看(过去 30 天)
Hi
Im doing data fitting and am interested in plotting a line from the data to the fitted curve, see wikipedia image.
Thats exactley what i want i just cant seem to plot the residuals like they have done.
This is my code
clear, clc
x = 0:pi/10:2*pi;
y = sin(x);
yx = random('ev',y,0.2);
plot(x,y,x,yx,'o'), axis([0-0.1 2*pi+0.1 min(yx)-0.1 max(yx)+0.1])
thanks

回答(2 个)

Matt Kindig
Matt Kindig 2012-1-5
Add to your code:
hold on;
line([x;x], [yx;y],'color','green');
plot(x,yx,'bo','markerfacecolor','blue');
You can tweak the colors as necessary.

Mamed
Mamed 2012-1-11
Thanks a lot work like a charm. I will post full code below for future readers.
clear, clc, clf
x = 0:pi/16:2*pi;
y = sin(x);
yx = random('norm',y,0.2);
subplot(2,1,1)
plot(x,y),hold on, axis([0-0.1 2*pi+0.1 min(yx)-0.1 max(yx)+0.1])
plot(x,yx,'r*')%,'markerfacecolor','red');
line([x;x],[yx;y],'LineStyle','-.', 'color','black');
hline = refline([0 0]), set(hline,'Color','k')
title('Curve Fitting Example')
legend('Fitted Curve','Sampled Values','Residuals'), hold on
subplot(2,1,2)
stem(x,yx-y,'b*'),axis([0-0.1 2*pi+0.1 min(yx)-0.1 max(yx)+0.1])
legend('Residuals'), title('Comparable Values of the Residuals')

Community Treasure Hunt

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

Start Hunting!

Translated by