for loop for the function

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 Comment

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.

Answers (1)

Jan
Jan on 24 Feb 2016
Edited: Jan on 25 Feb 2016
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 Comments

Thanks Jan ! But then when I run the function I got all my answer -inf and I don't know why....
Jan
Jan on 25 Feb 2016
Edited: Jan on 25 Feb 2016
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.

This question is closed.

Tags

Asked:

on 24 Feb 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!