Linear regression model was working...save script file now its not???
Show older comments
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
Kevin Cere
on 30 Sep 2011
Answers (1)
bym
on 1 Oct 2011
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
Kevin Cere
on 1 Oct 2011
Kevin Cere
on 1 Oct 2011
Kevin Cere
on 1 Oct 2011
bym
on 1 Oct 2011
I am not sure I agree with your statement about reducing error. Does changing m = size(X,1) give you the results you expect?
Kevin Cere
on 1 Oct 2011
Kevin Cere
on 1 Oct 2011
Categories
Find more on Parametric Modeling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!