Linear regression model was working...save script file now its not???

calculating youngs modulus and poisson's ratio from data in an excel file, everything was working perfectly then I saved the script file and boom now I get size errors inside my lsquares function, really frustating. If I change m=size(X,Y); to m=size(X,2); the script will run but the fuction no longer grabs the correct dimensions and the answers come out dead wrong. here is the scipt for both, forgive the lack of programing comments usually make them last. I know it's kinda hard (implaussible) to run the code without the excel file but I'd be more that willing to share it if need be.
.m*
function [a,b,c] = lsquares(X,Y)
p=polyfit(X,Y,1);
m=size(X, Y);
for i=1:m
p=polyfit(X(i),Y(i),1);
yfit=p(1)*X(i)+p(2);
yresid=Y(i)-yfit;
SSresid=sum(yresid.^2);
SStotal=(length(Y)-1)*var(Y);
rsq=1-SSresid/SStotal;
end
a=p(1);
b=p(2);
c=rsq;
_MAELab3.m_ %%%%%%
clear all;
Raw_Data=xlsread('Lab2.xlsx'); %read in data sheet
load=Raw_Data(:,2); %Load
long_strain=Raw_Data(:,3:4)/10^6; %long strain read in
lat_strain=Raw_Data(:,5:6)/10^6; %Lat strain read in
area=Raw_Data(:,7); %area read in
avg_long_strain=mean(long_strain,2); %avg long_strain
avg_lat_strain=mean(lat_strain,2); %avg lat_strain
stress=load./area; %calculate stress
[k,d]= lsquares(avg_long_strain,stress) %fuction call for least square for stress strain
[m,n]=lsquares(avg_long_strain,avg_lat_strain) %function call for least square for poisson ratio
y=(k*avg_long_strain+d)%LSR line
p=(m*avg_lat_strain+n)
%plot LSR and calculated vs experimental data
plot(avg_long_strain,stress,'ro'),hold on, plot(avg_long_strain,y,'bd-'),title('Stress vs Long Strain'),
xlabel('Strain'),ylabel('Stress(psi)'),legend('Experimental','Calculated')
figure;
plot(avg_lat_strain,avg_long_strain,'g+'),hold on, plot(avg_lat_strain,p,'bl-'),
title('Lateral Strain vs Longitudinal Strain'),xlabel('Lateral Strain'),ylabel('Longitudinal Strain'),
legend('Experimental','Calculated')
disp('Calculated Youngs Modulus is:'),k/10^9
disp('Calculated Poissons ratio is:'),-m

1 Comment

Here is the error that is being thrown I have tried numerous things and it still is cropping up. About to throw the function out and start from scratch.....
??? Error using ==> size
Dimension argument must be a
positive integer scalar within
indexing range.
Error in ==> lsquares at 2
m=size(X, Y);
Error in ==> MAE225Lab3 at 12
[k,d]=
lsquares(avg_long_strain,stress)
%fuction call for least square
for stress strain

Sign in to comment.

Answers (1)

The second argument for size() should be an integer, yet you are passing it Y. I am not sure what you want to accomplish in the for loop, but it is unnecessary:
function [a,b,c] = lsquares(X,Y)
p=polyfit(X,Y,1);
yfit = polyval(p,X);
yresid = Y-yfit;
SSresid = sum(yresid.^2);
SStotal = (length(Y)-1)*var(Y);
rsq = 1-SSresid/SStotal;
a=p(1);
b=p(2);
c=rsq;

6 Comments

the for loop is used to reduce error in the slope and y intercept,each iteration take the current value (previous) and runs it through the iteration, it does this for each value of X and Y in each of thoughs column vectors. I could also write this as a while loop but the answers don't fit. when passing to size(X,Y), X is a positive column vector and y is a + column vector. Change anything?
I use the slope of the LSR as my youngs modulus and poission ration calculation
Load P(Lbs) 1)Strain long (10^-6) 2)Strain long (10^-6) 1)Strain lateral (10^-6) 2)Strain lateral (10^-6) area in^2
200 0.000064 0.000125 -0.000024 -0.000042 0.2831166
400 0.000141 0.00023 -0.000052 -0.000077 0.2831166
600 0.000222 0.000327 -0.000082 -0.000109 0.2831166
800 0.000305 0.000423 -0.000114 -0.000141 0.2831166
1000 0.000394 0.000522 -0.000147 -0.000174 0.2831166
This is the data in the execl file so about the formating but maybe if someone can run it with this and get it to work. It WAS working perfectly before the save just makes no sense to me why it doesn't now.
I am not sure I agree with your statement about reducing error. Does changing m = size(X,1) give you the results you expect?
no it reduced the slop value to 0 by removing the Y dimension, I rewrote the code, and no my graphs are good but error in the poisson ratio call is at like 3000% the end answer numerically is correct but wow is the experimental and calculated error off. here is the new code.
MATLAB code
function [a, b,RMS]=lsquares(X, Y)
sx=0; %(sx is a variable for sum of x-values)
sy=0; %(sy is a variable for sum of y-values)
sxy=0; %(sxy is a variable for sum of products of x and y-values)
sxsq=0; %(sxsq is a variable for sum of squares of x-values)
sysq=0; %(sysq is a variable for sum of squares of y-values)
dn=0;
for i=1:5
sx=sx+X(i);
sy=sy+Y(i);
sxsq=sxsq+X(i)^2;
sysq=sysq+Y(i)^2;
sxy=sxy+X(i)*Y(i);
dn(i)=sy-((i*sxy-sx*sy)/(i*sxsq-sx^2)*X(i)+(sxsq*sy-sxy*sx)/(i*sxsq-sx^2));
E=(dn(i))^2;
end
a=(5*sxy-sx*sy)/(5*sxsq-sx^2);
b=(sxsq*sy-sxy*sx)/(5*sxsq-sx^2);
RMS=sqrt(E/i);
MATLAB code
Erro is 300+% I appologise for the sigdig misread ;-p
also here is the graph of the Poisson's ratio call.
MATLAB code
function createfigure(X1, YMatrix1)
%CREATEFIGURE(X1,YMATRIX1)
% X1: vector of x data
% YMATRIX1: matrix of y data
% Auto-generated by MATLAB on 01-Oct-2011 13:43:24
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1);
box(axes1,'on');
hold(axes1,'all');
% Create multiple lines using matrix input to plot
plot1 = plot(X1,YMatrix1,'Parent',axes1);
set(plot1(1),'Marker','+','LineStyle','none','Color',[0 1 0],...
'DisplayName','Experimental');
set(plot1(2),'Color',[0 0 1],'DisplayName','Calculated');
% Create title
title('Lateral Strain vs Longitudinal Strain');
% Create xlabel
xlabel('Lateral Strain');
% Create ylabel
ylabel('Longitudinal Strain');
% Create legend
legend(axes1,'show');

Sign in to comment.

Asked:

on 30 Sep 2011

Community Treasure Hunt

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

Start Hunting!