Integrating an anonymous function in a for loop

Hello All,
I have a relatively simple task. I am trying to integrate an anonymous function in a for loop. The following is the code:
%% Mode Shape Test %%
wn1 = 3.516015284068505; c1=-0.73410;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% What I am Trying to Accomplish with a For Loop %%
% Phi_1 = @(z)(-(cos(sqrt(wn1)*z) - cosh(sqrt(wn1)*z) + c1*(sin(sqrt(wn1)*z) - sinh(sqrt(wn1)*z)))).^1;
% Phi_2 = @(z)(-(cos(sqrt(wn1)*z) - cosh(sqrt(wn1)*z) + c1*(sin(sqrt(wn1)*z) - sinh(sqrt(wn1)*z)))).^2;
% Phi_3 = @(z)(-(cos(sqrt(wn1)*z) - cosh(sqrt(wn1)*z) + c1*(sin(sqrt(wn1)*z) - sinh(sqrt(wn1)*z)))).^3;
% Phi_4 = @(z)(-(cos(sqrt(wn1)*z) - cosh(sqrt(wn1)*z) + c1*(sin(sqrt(wn1)*z) - sinh(sqrt(wn1)*z)))).^4;
%
% g1=integral(Phi_1,0,1); % g1 = 0.7829
% g2=integral(Phi_2,0,1); % g2 = 0.9999
% g3=integral(Phi_3,0,1); % g3 = 1.4778
% g4=integral(Phi_4,0,1); % g4= 2.3486
% % I have to do this maybe over 100 times and it would be convinient to do
% % this in a loop structure.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% For loop Attempt %%
for i=1:4
Phi_(i) = @(z)(-(cos(sqrt(wn1)*z) - cosh(sqrt(wn1)*z) + c1*(sin(sqrt(wn1)*z) - sinh(sqrt(wn1)*z)))).^i;
g(i) = integral(Phi_(i),0,1);
end
The commented top section is the way that I have been doing it which works just fine, but I will now need the values for over Phi_100 and so on. When I try to run the For loop I get the following error:
Error using integral (line 82)
First input argument must be a function handle.
Error in Mode_Shape_Test (line 19)
g(i) = integral(Phi_(i),0,1);
Any Ideas?
Thanks
Christopher Reyes

 Accepted Answer

g=zeros(1,4); % preallocate
%% For loop Attempt %%
for i=1:4
Phi = @(z)(-(cos(sqrt(wn1)*z) - cosh(sqrt(wn1)*z) + c1*(sin(sqrt(wn1)*z) - sinh(sqrt(wn1)*z)))).^i;
g(i) = integral(Phi,0,1);
end
g % - > results saved

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!