How to call a function in a for loop in MATLAB? - matlab

I would like to call (execute) an m-file (function) in a loop like this:
global m, r
m = 2;
for n = 2:10;
for r1 = 0:n-m;
r2 = n-m-r1;
r = [r1,r2];
[Call the function here?????????]
end
end
This is the function:
function main
x0 = [-0.5403,0.5471];
fsolve(#fcn,x0)
function z = fcn(X)
rand('twister',5409);
global m, r
a = rand(m,1);
for i = 1:m
sm(i) = 0.0;
for l = m-i+1:m
sm(i) = sm(i)+r(l);
end
s = 1.0/(i+sm(i));
g(i) = (a(i))^s;
end
prod = 1.0;
for k = 1:m
prod = prod * g(m+1-k);
u(k) = 1.0-prod;
x(k) = (sqrt(3)/pi)*log(u(k)/(1-u(k)));
end
sum = 0;
sum1 = 0;
sum2 = 0;
for j = 1:m
sum = sum+(r(j)+2)*(1/(1+exp((-pi/sqrt(3))*((x(j)-X(1))/X(2)))));
sum1 = sum1+(r(j)+2)*((x(j)-X(1))/X(2))*(1/(1+exp((-pi/sqrt(3))*((x(j)-X(1))/X(2)))));
sum2 = sum2+(x(j)-X(1))/X(2);
end
z(1) = pi/(X(2)*sqrt(3))*(-m+sum);
z(2) =(-1/X(2))*(m+(pi/sqrt(3))*(sum2-sum1));
Thank you very much for your help.

The functions main and fcn should be saved in a file called "main.m". You have to make sure this file is either in your current working directory or somewhere on the MATLAB path (as mentioned in a comment by Amro) so that MATLAB can get to it. Since main requires no input arguments and has no output arguments, you could then just call it in any one of the following ways:
main
main;
main()
main();

If you have a function Main.m
Main.m
function out = main(in)
% blah blah blah
You would call the function
in = 2;
out = main(in)
Does this make sense?

Personnally I would create your function without a main() part.
create a file called
fcn.m
with your function fcn in it, make sure it's in your working directory or in your matlab path and then call it inside your loop.
addpath(genpath('/the/path/to/your/function/');
global m, r
m = 2;
for n = 2:10;
for r1 = 0:n-m;
r2 = n-m-r1;
r = [r1,r2];
z=fcn(r)
end
end

Related

Why does this not correctly evaluate e^x using the Taylor series?

I am attempting to write a function called expSeries which uses another function factFunc to evaluate e^x. I have already written the function factFunc, as shown below:
function fact = factFunc(n)
f = 1;
for a = 1:b
f = f*a;
end
fact = f;
end
I am now attempting to write the function expSeries which evaulates e^x using the Taylor series. This is what I have so far:
function expo = exponentialFunc(x)
terms = input('Enter the number of terms');
b = 0;
for i = 1:terms
b = x/factFunc(terms);
end
expo = b;
end
And in the main program, I have
n = exponentialFunc(4);
disp(n);
Where in this instance I am trying to find e^4. However, the output is not what expected. Does anyone have any idea where I am going wrong?
Fix to factFunc:
function fact = factFunc(n)
f = 1;
for a = 1:n
f = f*a;
end
fact = f;
end
Fix to exponentialFunc
function expo = exponentialFunc(x)
terms = input('Enter the number of terms');
b = 0;
for i = 0:terms-1
b = b + x^i/factFunc(i);
end
expo = b;
end
Example
>> exponentialFunc(4)
Enter the number of terms10
ans =
54.1541
Note exp(4) = 54.59815...

how do I save the result of loop for in a matrix?

I need to save results of a for loop in a matrix where its size is 4*1?
My function:
function test()
for j=2:2:8
h= 3*j
end
end
results:
h=6
h=12
h=18
h=24
Thank you in advance.
You can do this via loop, and than you should first create the matrix:
function test()
n = 2:2:8;
h = zeros(length(n),1)
counter = 1;
for j = n
h(counter) = 3*j
counter = counter+1;
end
end
But that is the long and non-efficient way, instead you should use vectorization:
n = 2:2:8;
h = n.'*3
or just: h = (2:2:8).'.*3;
that's it.

Anonymous function defined by a loop in matlab

I use anonymous functions in my code. For example:
G = #(x) [norm(A*x-b);A'*(A*x-b)];
norm(Ax-b) is the objective function and A'*(Ax-b) the gradient.
Then,
Algo( G,varagin );
etc
What I would like to do is to define f with a loop:
n = 9;
k = 2;
t = 1 - x.^k;
f = 0;
for i=1:n
f = f + x(i,1)*prod(t(1:i-1));
end
grad_f = zeros(n,1);
for i0=1:n
s = t;
s(i0) = [];
for i=i0+1:n
grad_f(i0) = grad_f(i0) + x(i)*prod(s(1:i0-1));
end
grad_f(i0) = -k*x(i0)^(k-1)*grad_f(i0);
grad_f(i0) = grad_f(i0) + prod(t(1:i0-1));
end
Then I would like to do something like:
" G = #(x) [f,grad_f] "
Thanks a lot for your help!
Found the answer:
create F(x) and GRAD_F(x) as functions in matlab computing f and grad_f respectively.
Then:
G = #(x) [F(x);GRAD_F(x)];
Algo(G,varargin);

Matlab: Create function with another function as argument

I need to create a function f with another function, g, as argument (g is defined in a .m file, not inline). In the body of f, I need to use feval to evaluate g on multiple values; something like:
function y = f(a,b,c,g)
z=feval(g,a,b,c);
y=...
end
What is the syntax ? I tried to use handles, but I got error messages.
You can do it this way:
Define f in an m-file:
function y = f(a,b,c,g)
y = feval(g,a,b,c);
end
Define g in an m-file:
function r = g(a,b,c)
r = a+b*c;
end
Call f with a handle to g:
>> f(1,2,3,#g)
ans =
7
If you do not want to change the body of add, then you could do that:
function s = add_two(a)
s = a + 2;
end
function s = add_three(a)
s = a + 3;
end
function s = add(a, fhandle1, fhandle2)
s = feval(fhandle1, a);
s = s + feval(fhandle2, s);
end
a = 10;
fhandle1 = #add_two; // function handler
fhandle2 = #add_three;
a = add(a, fhandle1, fhandle2);
a

Matlab. Create a loop to change variable size with each iteration

I am currently trying to run a script that calls a particular function, but want to call the function inside a loop that halfs one of the input variables for roughly 4 iterations.
in the code below the function has been replaced for another for loop and the inputs stated above.
the for loop is running an Euler method on the function, and works fine, its just trying to run it with the repeated smaller step size im having trouble with.
any help is welcomed.
f = '3*exp(-x)-0.4*y';
xa = 0;
xb = 3;
ya = 5;
n = 2;
h=(xb-xa)/n;
x = xa:h:xb;
% h = zeros(1,4);
y = zeros(1,length(x));
F = inline(f);
y(1) = ya;
for j = 1:4
hOld = h;
hNew = hOld*0.5;
hOld = subs(y(1),'h',hNew);
for i = 1:(length(x)-1)
k1 = F(x(i),y(i));
y(i+1,j+1) = y(i) + h*k1;
end
end
disp(h)
after your comment, something like this
for j = 1:4
h=h/2;
x = xa:h:xb;
y = zeros(1,length(x));
y(1) = ya;
for i = 1:(length(x)-1)
k1 = F(x(i),y(i));
y(i+1,j+1) = y(i) + h*k1;
end
end