MATLAB 不会返回我的输出

标签 matlab

我在 MATLAB 中编写了一个函数,但它不会返回我的输出。它返回第一个输出作为 ans=a # 但仅此而已。我认为 function[argout1,argout2,...argoutn=funcname(in1,in2,...inn) 返回格式为 argout1= a #, argout2= a # 等等。

我意识到我可以使用 disp() 或其他一些内置函数,但是当你有一个函数文件时,这一切有什么意义。该函数正在访问 Excel 电子表格中的输入,但我认为这不会产生影响。任何帮助将不胜感激。

代码如下。

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')

最佳答案

假设你有一个函数fun:

[o1, o2, o3, o4, o5] = fun(i1, i2, i3, i4);

如果执行

fun(in1, in2, in3)

只有一个输出参数:ans 默认输出参数。这将获取函数头中定义的第一个输出参数的值(即 o1)。

如果您想要更多输出,则必须指定它们:

[out1, out2, out3] = fun(in1, in2, in3);

现在 out1out2out3 将分别包含函数中分配给 o1o2o3

您绝对应该阅读function documentation .

关于MATLAB 不会返回我的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12650533/

相关文章:

Matlab。将图例放在图外

matlab - 我应该在 MATLAB 中的属性名称后加上分号吗?

Matlab/Octave - 开始使用函数时遇到问题,是否有一个函数 main 等效?

matlab - 如何直接从 table.Properties.VariableNames 获取逗号分隔的列表?

matlab - 找到图像 : Matlab 一系列曲线的交点

matlab - 检测到的 Windows MATLAB 上的非最大抑制

arrays - 在 Matlab 中使用 hist 计算出现次数

windows - 如何使用 MATLAB 中的 Cygwin 执行其他目录中的命令?

matlab - Matlab 中结构内的元胞数组 - 可能吗?

r - 在 Matlab 和 R 之间共享大型数据集