Error using plot Vectors must be the same length

x1 = [0,3,6,9,12,15,18,21,24,27,30,33,36,39,41,44,47,50,53,56,59,62]; y1 = [1.4750,1.0873,0.7613,0.5446,0.3883,0.2920,0.1983,0.1416,0.1000,0.0710,0.0527,0.0360,0.0253,0.0186,0.0136,0.0093,0.0060,0.0041,0.0030,0. 0020,0, 0016,0.0010];
x2 = [0,3,6,9,12,15,18,21,24,27,30,33,36,39,41,44,47,50,53,56,59,62]; y2 = [0.3887,0.0837,-0.2731,-0.6077,-0.9460,-1.2658,-1.6180,-1.9547,-2.3026,-2.6451,-2.9431,-3.3242,-3.6770,-3.9846,-4.2977,-4.6777,-5.1160,-5.7763,-5.8091,-6.2146,-6.4377,-6.9077]
n = length(x); sumx=0.0; sumy=0.0; sumxy=0.0; sumxx=0.0;
for i=1:n sumx=sumx+x(i); sumy=sumy+y(i); sumxy=sumxy+x(i)*y(i); sumxx=sumxx+x(i)*x(i); end
%Determine Slope and y-intercept slope=(n*sumxy-sumx*sumy)/(n*sumxx-sumx^2); y_int=(sumxx*sumy-sumxy*sumx)/(n*sumxx-sumx^2);
z=slope*x+y_int;
%Making a plot loaded data plot(x1,y1,'bo',x,z,'r-') hold on Plot(x2,y2,’bo’,x,z,’r-‘) Hold off xlabel("time(s)") ylabel("v(t) v") %print('RC.png','-dpng')
%Errors di=y-(slope*x+y_int);
Error_in_slope=sqrt(sum(di.*di)*(n/(n-2))/(n*sumxx-sumx^2)); Error_in_y_int=sqrt(sum(di.*di)*sumxx*(n/(n-2))/(n*sumxx-sumx^2));

2 Comments

VBBV
VBBV on 9 Oct 2020
Edited: VBBV on 9 Oct 2020
vector y1 has more elements than x1, x2 and y2
declare the vectors x, y, ... are they same as x1 and y1 ...?
Then run the loop and plot it
Please reformat your code. Go into the editor and select the code and press the ">" button.... and then go through it and put in the appropriate line breaks, as we cannot assume which parts are really commented out and which just look to be commented out due to bad formatting.

Sign in to comment.

 Accepted Answer

for i=1:n
sumx=sumx+x(i);
sumy=sumy+y(i);
sumxy=sumxy+x(i)*y(i);
sumxx=sumxx+x(i)*x(i);
end
Toa chieve above you need not run a loop..you can achieve that using:
sumx = sum(x) ;
sumy = sum(y) ;
sumxy = sum(x.*y) ;
sumxx = sum(x.*x) ;
Note that to get sumxy, the dimensions of x and y should be same.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!