I have written a function in MATLAB but it will not return my outputs. It returns the first output as ans=a # but that’s it. I thought function[argout1,argout2,...argoutn=funcname(in1,in2,...inn) returns all outputs in the format argout1= a #, argout2= a # and so on.
I realize I could use disp() or some other built in function but what is the point in all that when you have a function file. The function is accessing the inputs from an excel spreadsheet but I didn’t think that should make a difference. Any help would be greatly appreciated.
The code is as follows.
function[y_int1, y_int2, Youngs_Modulus, Poissons_Ratio, Youngs_Modulus_Percent_diff, Poissons_Ratio_Percent_diff, RMS1, RMS2]= Lab3_EC(elong1,elong2,elat1,elat2,thick,width,load,N)
%average longitudinal strain
avgLongs=((elong1)+(elong2))/2;
%average lateral strain
avgLats=((elat1)+(elat2))/2;
%Find's the area of the sample
A=thick*width;
%Finds the applied stress
stress=load./A;
%declares a variable x1 for Long Strain
x1=avgLongs;
%declares a variable y1 for Applied Stress
y1=stress;
%Declares variables to compute slope1
x1bar=mean(x1);
y1bar=mean(y1);
sxy1=sum(x1.*y1);
sx1=sum(x1);
sy1=sum(y1);
sx1s=sum(x1.*x1);
sx11s=(sum(x1))^2;
slope1=(N*sxy1-sx1*sy1)/(N*sx1s-sx11s);
y_int1=y1bar-slope1*x1bar;
%x points
xp=linspace(min(x1),max(x1));
% y points
yp1=slope1*xp+y_int1;
%Declares variables to compute slope 2
%Since our second calculation of linear regression uses Average
%Longitudinal Stress we can re-use the variable x1
y2=abs(avgLats);
y2bar=mean(y2);
sxy2=sum(x1.*y2);
sy2=sum(y2);
slope2=(N*sxy2-sx1*sy2)/(N*sx1s-sx11s);
y_int2=y2bar-slope2*x1bar;
%y points
yp2=slope2*xp+y_int2;
%preallocate space for error1 vector
Error1=zeros(N,1);
%compute error1
for i=1:N;
Error1(i)= (stress(i)-y_int1-slope1*avgLongs(i))^2;
end
Error1=sum(Error1(5));
%preallocate space for error2 vector
Error2=zeros(N,1);
for i=1:N;
Error2(i)=(abs(avgLats(i))-y_int2-slope2*avgLongs(i))^2;
end
Error2=sum(Error2(5));
RMS1=sqrt(Error1/N);
RMS2=sqrt(Error2/N);
Lab_2_E=(stress(5)-stress(1))/(avgLongs(5)-avgLongs(1));
Lab_2_v=(abs(avgLats(5)-avgLats(1))/(avgLongs(5)-avgLongs(1)));
Youngs_Modulus=slope1;
Poissons_Ratio=slope2;
Youngs_Modulus_Percent_diff=((abs(Youngs_Modulus-Lab_2_E))/Lab_2_E)*100;
Poissons_Ratio_Percent_diff=((abs(Poissons_Ratio-Lab_2_v))/Lab_2_v)*100;
%Plot stress strain diagram and poisson's ratio
figure(1), plot(avgLongs,stress,'k',x1,y1,'o',xp,yp1,':g'),hold on,...
xlabel('Long Strain(in/in)'),ylabel('Stress(psi)'),...
title('Stress vs. Long Strain'), legend('Exp','data points','Calc',...
'Location','SouthEast')
figure(2),plot(avgLongs,abs(avgLats),x1,y2,'+',xp,yp2,'-- m'),hold on,...
xlabel('Long Strain(in/in)'),ylabel('Lat Strain(in/in)'),...
title('Poisson''s Ratio'),legend('Exp','data points','Calc',...
'Location','SouthEast')
Assume you have a function
fun:If you execute
there is only one output argument:
ansthe default output argument. This’ll get the value of the first output argument defined in your function header (namelyo1).If you want more outputs, you have to specify them:
Now
out1,out2andout3will respectively contain the values assigned in the function too1,o2ando3.You should definitely read through function documentation.