Info

此问题已关闭。 请重新打开它进行编辑或回答。

for loop for the function

7 次查看(过去 30 天)
Coleman
Coleman 2016-2-24
关闭: MATLAB Answer Bot 2021-8-20
Hi all, here is my function
function [error] =stress5_function(Ke,n,Sult,u,t,S0,k,m)
pa=0.1013 ;
S3=5 ;
Ei=Ke.*pa.*(S3./pa).^n;
Rf=(S0+k.*exp(-(S3./m)))./(Sult+u.*exp(-(S3./t)));
% z=(£m1-£m3)
z=0:0.0001:5;
Et=Ei.*(1-(Rf.*z)./(S0+k.*exp(-(S3./m))));
Et(Et<0)=0;
x=(z./Et).*100;
A=xlsread('5MPa','A1:A32');
B=A';
C=xlsread('5MPa','B1:B32');
D=C';
startingIndex = find(x==0);
endingIndex = find(x>=16);
desiredX = x(startingIndex:endingIndex);
desiredZ = z(startingIndex:endingIndex);
A1 = trapz(desiredX,desiredZ);
A2= trapz(B,D);
error= ((A1-A2)./A1)*100;
end
there are 8 parameters in the function and the value of the parameters, I stored in an excel file
Ke=xlsread('parameters','A2:A9096');
n=xlsread('parameters','B2:B9096');
Sult=xlsread('parameters','C2:C9096');
u=xlsread('parameters','D2:D9096');
t=xlsread('parameters','E2:E9096');
S0=xlsread('parameters','F2:F9096');
k=xlsread('parameters','G2:G9096');
m=xlsread('parameters','H2:H9096');
pa=0.1013 ;
S3=5 ;
I want to create a for loop to run the function what should I write??
  1 个评论
Stephen23
Stephen23 2016-2-24
Do NOT call any variable or function error, because then you stop the very important inbuilt function error from working properly. For the same reason you should never name any variable or function size, length, cell, table, i, j, etc.

回答(1 个)

Jan
Jan 2016-2-24
编辑:Jan 2016-2-25
Your funtion accepts vectors as input. So there is no need to call it through a for loop. But if you really want to do this:
err = zeros(size(Ke));
for i = 1:numel(Ke)
err(i) = stress5_function(Ke(i),n(i),Sult(i),u(i),t(i),S0(i),k(i),m(i));
end
Better read the 5MPa file once only and provide the contents as inputs:
B = xlsread('5MPa','A1:A32').';
D = xlsread('5MPa','B1:B32').';
  2 个评论
Coleman
Coleman 2016-2-25
Thanks Jan ! But then when I run the function I got all my answer -inf and I don't know why....
Jan
Jan 2016-2-25
编辑:Jan 2016-2-25
Use the debugger to step throught your code line by line, until you find the reason for the unexpected values. Please note, that we cannot do this for you, because we do not have the input data and cannot guess, why you assume a vlue different from Inf.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by