Reputation: 321
I want to call multiple dependent functions like this in the code.
function [k11,k12] = k_fun()
% syntax is : function [outputs] = function-name(inputs)
a=2.0;
k11 = quad(B1,B1,a); %error
% For k11, I want to call function b_fun and select only B1,B2 to pass to function quad.
k12 = quad(B1,B2,a);
endfunction
function [B] = b_fun(x)
B1 = -0.5+x;
B2 = 2.0*x;
B3 = 0.5+x;
B=[B1,B2,B3];
endfunction
function [value] = quad(B_i,B_j,a)
value=0
points = [0.57,-0.57];
wt=[1.0,1.0]
for (ct=1:1:2)
value = value + (B_i(points(ct))*B_j(points(ct))*a)*wt(ct);
end
endfunction
I want to run function k_fun. After a=2.0
It will go to k11 line. For k11
, I want to get B1
and B2
from b_fun()
, pass it to quad()
. In quad()
, the functions will be evaluated.
I am trying to do
k12 = B1(0.57)*B2(0.57)*a*1 + B1(-0.57)*B2(-0.57)*a*1
. These values, +-0.57, come in the function quad
. I am not getting how to pass B1
and B2
in the line %error
. I am getting stuck in invoking dependent functions. I want pretty much to keep same program format (variables defined in the respective functions) because the original program goes like this. Later, I want to translate this program to C++, so would like to use standard functions of programming languages instead of specific ones.
By the way, I started the program in Octave, so endfunction
instead of end
in MATLAB.
Upvotes: 2
Views: 85
Reputation: 30047
Here's another method:
B = @(x) [-0.5 + x ;
2.0 .* x ;
0.5 + x ];
Now you can pass a row vector to B
and get a column of outputs! So
points = [0.57,-0.57];
B(points) =
0.0700 -1.0700
1.1400 -1.1400
1.0700 -0.0700
Then you say you want to actually compute:
k12 = B1(0.57)*B2(0.57)*a*1 + B1(-0.57)*B2(-0.57)*a*1
Which, under this new structure would be
k12 = B(1,1)*B(2,1)*a*1 + B(1,2)*B(2,2)*a*1
I'm not sure why you've left the *1
s in, but I've kept them for completeness. In summary, you only have to have one B_output = B(points)
variable, and B2
is now accessed via B_output(2,i)
, where i
is the column index corresponding to the points
index. If you only passed B(x)
one point, you could say B2 = B_output(2)
!
To summarise
Your entire code could be written:
a = 2.0;
B = @(x) [-0.5 + x ;
2.0 .* x ;
0.5 + x ];
k11 = quad(B, 1, 1, a);
k12 = quad(B, 1, 2, a);
function value = quad(B, i, j, a)
value = 0;
points = [0.57,-0.57];
wt = [1.0,1.0];
B_output = B(points);
for ct = 1:2
value = value + B_output(i, ct)*B_output(j, ct)*a*wt(ct);
end
end
Upvotes: 0
Reputation: 65430
Your best bet is going to be to reorganize a little bit so that you have a separate version of b_fun
for each B*
variable. You can then create a function handle to these b_fun
's and pass that to quad
.
function [k11, k12] = k_fun()
a = 2.0;
k11 = quad(@b1_fun, @b1_fun, a);
k12 = quad(@b1_fun, @b2_fun, a);
end
function result = b1_fun(x)
result = x - 0.5;
end
function result = b2_fun(x)
result = 2 * x;
end
Alternately, you can just create B1
and B2
anonymous functions since they are so straightforward
function [k11, k12] = k_fun()
a = 2.0;
B1 = @(x)x - 0.5;
B2 = @(x) 2 * x;
k11 = quad(B1, B1, a);
k12 = quad(B1, B2, a);
end
Upvotes: 1