How to use statement functions with arrays in Fortran 90 - matlab

I am translating a Matlab code into Fortran 90 and am trying to translate the following piece of code:
func= inline('x+ y+ z', 'x', 'y', 'z')
x(1)= 1, y(1)= 1, z(1)= 1
for n= 1:5
output= 5+ func(x(n), y(n), z(n))
x(n+ 1)= x(n)+ 1
y(n+ 1)= y(n)+ 1
z(n+ 1)= z(n)+ 1
end
In Fortran I am using the statement (inline) function as func(x, y, z)= x+ y+ z, however, I am not able to insert the array part into the function.
How can I negotiate the dependence on 'n' into the statement function? I am trying something like the following but am not quite there yet.
func(x, y, z)= x+ y+ z
x(1)= 1, y(1)= 1, z(1)= 1
do n= 1, 5
func(x(n), y(n), z(n))= x(n)+ y(n)+ z(n)
end
Any help would be greatly appreciated.

This is an extended and formatted comment rather than an answer.
The general advice with statement functions in modern Fortran is Don't, just don't. They're not big and they're not clever. They were also deprecated in, I think, the Fortran 90 standard so, to be pedantic, your requirements are inconsistent.
Beyond that, it's difficult to provide any specific advice. If I understand the Matlab correctly the code you show is a convoluted way to calculate 5+3*1+3*2+...+3*5. In Fortran 90 you might write
sum([5, (3*k,k=1,5)])
to calculate that.
Perhaps if we knew more of the context of your problem we'd be able to provide better advice.

I did realize that statement functions in modern Fortran is not the way to go. Instead I have created subroutines for the functions and then call them in the loop. This way all the values of the function in the loop can be put into an array (instead of just the last values). The correct piece of code looks like this:
subroutine funcsub(func, x, y, z, funcn)
implicit none
real, dimension(funcn), intent(out) :: func
real, dimension(funcn), intent(in) :: x, y, z
integer, intent(in) :: funky
func= x+ y+ z
end subroutine funcsub
do i= 1, 5
funcn= size(func)
call funcsub(func, x(i), y(i), z(i), funcn)
x(i+ 1)= x(i)+ 1
y(i+ 1)= y(i)+ 1
z(i+ 1)= z(i)+ 1
end do

Related

Plotting graph in maple

I've been trying to plot a graph of function f(x) on interval 0,6. For some reason maple doesn't plot the graph when I use 'f(x)' as an argument. It works fine when I substitute assigned function as an argument. What can be the reason? How can I plot it using 'f(x)' as an argument?
My code is as follows and the error is on the pictures:
mystep:=proc(x,a,b,c)
if x<a
then
b;
else
c;
end if:
end proc:
f(x):=mystep(x,3,-2,7);
plot('f(x)', x=0..6);
enter image description here
Your syntax for creation of the operator (that you hope to assign to f) is incorrect for 1D plaintext input.
Here is the usual syntax for creation of such an operator, and assignment to name f.
restart;
mystep:=proc(x,a,b,c)
if x<a then b; else c; end if:
end proc:
f:=x->mystep(x,3,-2,7);
These should now all work as you expect.
plot('f(x)', x=0..6);
plot(f, 0..6);
plot(x->mystep(x,3,-2,7), 0..6);
In recent versions of Maple the syntax that you used can work in (only) 2D Input mode. But it is a poor syntax to use, since is easily confused with the 1D syntax for assigning into the remember table of an operator (and with even more confusion ensuing). You should avoid ambiguous syntax.
The type of function you have is piecewise-defined function (see this wikipedia page https://en.wikipedia.org/wiki/Piecewise). And in Maple there is already a command for defining this type of a function, piecewise, see its help page for a complete explanation of how to use it. Now for your mysetup, you have a condition x < a, and a value for when this happens, b, and a value for otherwise, c. So you want piecewise( x < a, b, c ). I think it is better to just use this command, but if it is really necessary to define a new procedure, then your mysetup becomes like the following.
mystep := proc(x, a, b, c)
return( piecewise( x < a, b, c ) ):
end proc:
Now for your plot you can use either
plot( piecewise( x < 3, -2, 7 ), x = 0..6 );
or
f(x) := mystep(x, 3, -2, 7);
plot( f(x), x = 0..6);

Issue with using fplot on a symbolic expression

Here is an example where fplot doesn't plot anything:
a=0.336;
Ta=9.476;
Te=1.208;
Tw=1.498;
eqh=[0.661;0.619;0.568];
ex=[-1.24;-1.346;-1.441];
en=-ex;
ey=[0.376;0.705;0.968];
eqx=[-0.309;-0.357;-0.392];
eh=[1.594;1.583;1.545];
eyqh=[0.642;0.78;0.897];
a0=a*Ta*Te^2;
syms bt Td Ki Kp;
a1=sym([]);a2=sym([]);a3=sym([]);a4=sym([]);exqh=sym([]);
for i=1:3
Kp=1/bt;
exqh(i)=en(i)*eqh(i)+eqx(i)*eh(i);
Ki=1/(bt*Td);
a1(i)=Ta*Tw*eqh(i)+a*Te^2*en(i)+a*Te^2*ey(i)*Kp;
a2(i)=a*Te^2*ey(i)*Ki+Ta+Tw*exqh(i)-eyqh(i)*Kp*Tw;
a3(i)=en(i)+ey(i)*Kp-eyqh(i)*Ki*Tw;
a4(i)=ey(i)*Ki;
assume(bt~=0)
f=#(bt) a1(i)
fplot(f,[0.01 1],'b')
hold on
end
And here is another example, where fplot works:
syms y x;
y=#(x) 2/x+6;
z=y;
assume(x~=0)
fplot(z,[-1 1],'b')
I cannot understand the difference between these two cases. In my opinion, they are the same.
Would anyone please explain why the top example doesn't work, but the bottom one does?
fplot requires an expression that returns a numeric value. f returns a sym (symbolic expression), so it doesn't work, whereas z returns a number - so it does. That's all the difference.
Note that in the working example, you overwrote the y sym on the 2nd row, which means you provided a "proper" function handle to fplot, "by mistake". In fact, you need much less code for that example to work:
fplot( #(x)2./x+6, [-1 1], 'b');
The easiest way to get the top code working is by using matlabFunction. In other words, you can fix your code by changing this line:
f=#(bt) a1(i)
to this:
f = matlabFunction(a1(i));

Integral of an integral

Why does the error message of this code return: "Subscript indices must either be real positive integers or logicals.", when I am using ceil for every subscript?
A=1:1:100;
B=1:1:100;
C=1;
D=1:1:100;
E=2;
F=1:1:100;
G=1:1:100;
H=0.1:0.1:10;
fun_1=#(t)integral(#(ti)G(ceil(ti)).*H(ceil(t-ti)),0.1,t-1);
fun_2=#(t)integral(#(ti)G(ceil(ti)).*B(ceil(ti)).*(C.*D(t).^E)./F(t).*...
exp(-integral(#(x)(C.*D(ceil(x)).^E)./F(ceil(x)),ti,5)-K.*(t-ti)),0.1,t-
1,'ArrayValued',true);
I=500;
J=1000;
K=2;
fun_3=#(t)I*integral(#(ti)min(fun_2(ceil(ti)),J).*exp(-(K+I).*(t-ti)),0.1,t-
1);
t=1:1:5;
figure(1)
fplot(fun_1,t);
figure(2)
fplot(fun_2,t);
figure(3)
fplot(fun_3,t);
fplot see documentation Called as fplot(f,xinterval) evaluates your function handle f over the interval xinterval. IT will evaluate f at automatically determined steps along that given interval.
From the docs:
xinterval — Interval for x [–5 5] (default) | two-element vector of
form [xmin xmax]
You seem to be trying to specify exactly where you want your functions evaluated
t=1:1:5;
...
fplot(fun_1,t);
But it doesn't work that way. What is happening is that fplot is evaluating the function from 1 to 2 (the first 2 elements of t). So for example it might feed values of t = 1, 1.05, 1.1,... ,2 into your fun_# functions.
You can tell this because you first function which does work actually plots over the x-range of 1 to 2.
The reason you are getting a subscript indices error is because in fun_2 you have this ...(C.*D(t).^E)./F(t).*... Since fplot is feeding in values for t which are spaced between 1 and 2 (ex. 1.1) that is not a valid index.
If you really just want the values of your functions at t = 1:1:5 The you probably do not want to use fplot and just want evaluate the functions at those times and plot it.
y = feval(fun_1,t);
plot(t,y)
EDIT: The above code doesn't work
You will need to do something like the code below. This is because the 2nd & 3rd trems to the intergral function need to be scalar (1x1). If you feed them an array for t then they crash. So evaluate at each t not all at once.
figure(1)
y_1 = arrayfun(fun_1,t);
plot(t,y_1);
figure(2)
y_2 = arrayfun(fun_2,t);
plot(t,y_2);
figure(3)
y_3 = arrayfun(fun_3,t);
plot(t,y_3);
Note: the Third function still errors ... and I'm not 100% sure why. I didn't really look at it.

Nested numerical integration

The problem in the link:
can be integrated analytically and the answer is 4, however I'm interested in integrating it numerically using Matlab, because it's similar in form to a problem that I can't integrate analytically. The difficulty in the numerical integration arises because the function in the two inner integrals is a function of x,y and z and z can't be factored out.
by no means, this is elegant. hope someone can make better use of matlab functions than me. i have tried the brute force way just to practice numerical integration. i have tried to avoid the pole in the inner integral at z=0 by exploiting the fact that it is also being multiplied by z. i get 3.9993. someone must get better solution by using something better than trapezoidal rule
function []=sofn
clear all
global x y z xx yy zz dx dy
dx=0.05;
x=0:dx:1;
dy=0.002;
dz=0.002;
y=0:dy:1;
z=0:dz:2;
xx=length(x);
yy=length(y);
zz=length(z);
s1=0;
for i=1:zz-1
s1=s1+0.5*dz*(z(i+1)*exp(inte1(z(i+1)))+z(i)*exp(inte1(z(i))));
end
s1
end
function s2=inte1(localz)
global y yy dy
if localz==0
s2=0;
else
s2=0;
for j=1:yy-1
s2=s2+0.5*dy*(inte2(y(j),localz)+inte2(y(j+1),localz));
end
end
end
function s3=inte2(localy,localz)
global x xx dx
s3=0;
for k=1:xx-1
s3=s3+0.5*dx*(2/(localy+localz));
end
end
Well, this is strange, because on the poster's similar previous question I claimed this can't be done, and now after having looked at Guddu's answer I realize its not that complicated. What I wrote before, that a numerical integration results in a number but not a function, is true – but beside the point: One can just define a function that evaluates the integral for every given parameter, and this way effectively one does have a function as a result of a numerical integration.
Anyways, here it goes:
function q = outer
f = #(z) (z .* exp(inner(z)));
q = quad(f, eps, 2);
end
function qs = inner(zs)
% compute \int_0^1 1 / (y + z) dy for given z
qs = nan(size(zs));
for i = 1 : numel(zs)
z = zs(i);
f = #(y) (1 ./ (y + z));
qs(i) = quad(f, 0 , 1);
end
end
I applied the simplification suggested by myself in a comment, eliminating x. The function inner calculates the value of the inner integral over y as a function of z. Then the function outer computes the outer integral over z. I avoid the pole at z = 0 by letting the integration run from eps instead of 0. The result is
4.00000013663955
inner has to be implemented using a for loop because a function given to quad needs to be able to return its value simultaneously for several argument values.

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

I am trying to calculate the convolution of
x(t) = 1, -1<=t<=1
x(t) = 0, outside
with itself using the definition.
http://en.wikipedia.org/wiki/Convolution
I know how to do using the Matlab function conv, but I want to use the integral definition. My knowledge of Matlab and WolframAlpha is very limited.
I am still learning Mathematica myself, but here is what I came up with..
First we define the piecewise function (I am using the example from the Wikipedia page)
f[x_] := Piecewise[{{1, -0.5 <= x <= 0.5}}, 0]
Lets plot the function:
Plot[f[x], {x, -2, 2}, PlotStyle -> Thick, Exclusions -> None]
Then we write the function that defines the convolution of f with itself:
g[t_] = Integrate[f[x]*f[t - x], {x, -Infinity, Infinity}]
and the plot:
Plot[g[t], {t, -2, 2}, PlotStyle -> Thick]
EDIT
I tried to do the same in MATLAB/MuPad, I wasn't as successful:
f := x -> piecewise([x < -0.5 or x > 0.5, 0], [x >= -0.5 and x <= 0.5, 1])
plot(f, x = -2..2)
However when I try to compute the integral, it took almost a minute to return this:
g := t -> int(f(x)*f(t-x), x = -infinity..infinity)
the plot (also took too long)
plot(g, t = -2..2)
Note the same could have been done from inside MATLAB with the syntax:
evalin(symengine,'<MUPAD EXPRESSIONS HERE>')
Mathematica actually has a convolve function. The documentation on it has a number of different examples:
http://reference.wolfram.com/mathematica/ref/Convolve.html?q=Convolve&lang=en
I don't know much about Mathematica so I can only help you (partially) about the Matlab part.
To do the convolution with the Matlab conv functions means you do it numerically. What you mean with the integral definition is that you want to do it symbolically. For this you need the Matlab Symbolic Toolbox. This is essentially a Maple plugin for Matlab. So what you want to know is how it works in Maple.
You might find this link useful (wikibooks) for an introduction on the MuPad in Matlab.
I tried to implement your box function as a function in Matlab as
t = sym('t')
f = (t > -1) + (t < 1) - 1
However this does not work when t is ob symbolic type Function 'gt' is not implemented for MuPAD symbolic objects.
You could declare f it as a piecewise function see (matlab online help), this did not work in my Matlab though. The examples are all Maple syntax, so they would work in Maple straight away.
To circumvent this, I used
t = sym('t')
s = sym('s')
f = #(t) heaviside(t + 1) - heaviside(t - 1)
Unfortunately this is not successful
I = int(f(s)*f(t-s),s, 0, t)
gives
Warning: Explicit integral could not be found.
and does not provide an explicit result. You can still evaluate this expression, e.g.
>> subs(I, t, 1.5)
ans =
1/2
But Matlab/MuPad failed to give you and explicit expression in terms of t. This is not really unexpected as the function f is discontinuous. This is not so easy for symbolic computations.
Now I would go and help the computer, fortunately for the example that you asked the answer is very easy. The convolution in your example is simply the int_0^t BoxFunction(s) * BoxFunction(t - s) ds. The integrant BoxFunction(s) * BoxFunction(t - s) is again a box function, just not one that goes from [-1,1] but to a smaller interval (that depends on t). Then you only need to integrate the function f(x)=1 over this smaller interval.
Some of those steps you would have to to by hand first, then you could try to re-enter them to Matlab. You don't even need a computer algebra program at all to get to the answer.
Maybe Matematica or Maple could actually solve this problem, remember that the MuPad shipped with Matlab is only a stripped down version of Maple. Maybe the above might still help you, it should give you the idea how things work together. Try to put in a nicer function for f, e.g. a polynomial and you should get it working.