MATLAB: Arrays of Anonymous Functions used in for loop

anonymous functionsarrays

I am trying to create and sum up Anonymous Functions using for loops so that I can plot it. Please help me to understand how to correct the syntax, or if this is even valid to do.
clear
clc
terms = input('How many terms are there for the loading? ');
L = input('What is the beam length? ');
disp('')
disp('A<X-B>^C')
wA = zeros(terms,1);
vA = zeros(terms,1);
B = zeros(terms,1);
wC = zeros(terms,1);
vC = zeros(terms,1);
shear = zeros(terms,1);
SHEAR = 0;
x = 0:1/(L*100):L;
%inputs the values of the static loading
for i = 1:terms
fprintf('\nThe term is %i\n',i)
wA(i) = input('The value of A: ');
B(i) = input('The value of B: ');
wC(i) = input('The value of C: ');
end
%calculates the singularity function for the shear
for j = 1:terms
if wC(j)>=0
vA(j) = wA(j)/(wC(j)+1);
vC(j) = wC(j)+1;
else
vA(j) = wA(j);
vC(j) = wC(j)+1;
end
end
%Combines the SHEAR terms into one function
for s = 1:terms
if vB(s) == L
s = s+1;
end
if vC(s) > 0
shear{s} = @(x) vA(s)*(x.^(vC(s)).*heaviside(x-vB(s));
elseif vC(s) == 0
shear{s} = @(x) vA(s).*heaviside(x-vB(s));
elseif vC(s)< 0
shear{s} = 0;
end
SHEAR = SHEAR(x) + shear{s}(x);
end
%PLOT OF SHEAR VS X
figure(1)
plot(x,SHEAR)
title 'Shear force as a function of X'
xlabel 'Shear'
ylabel 'Length'

Best Answer

It is not valid. Here is one way that I might rewrite the code,
terms = input('How many terms are there for the loading? ');
L = input('What is the beam length? ');
disp('')
disp('A<X-B>^C')
[wA,vA,wB,vB,wC,vC]=deal(zeros(terms,1));
%inputs the values of the static loading
for i = 1:terms
fprintf('\nThe term is %i\n',i)
wA(i) = input('The value of A: ');
wB(i) = input('The value of B: ');
wC(i) = input('The value of C: ');
end
%calculates the singularity function for the shear
for j = 1:terms
if wC(j)>=0
vA(j) = wA(j)/(wC(j)+1);
vC(j) = wC(j)+1;
else
vA(j) = wA(j);
vC(j) = wC(j)+1;
end
end
%PLOT OF SHEAR VS X
figure(1)
x = 0:1/(L*100):L;
plot(x,shearCalc(x, vA,vB,vC,L,terms))
title 'Shear force as a function of X'
xlabel 'Shear'
ylabel 'Length'
function shearTotal=shearCalc(x, vA,vB,vC,L,terms)
shearTotal=0;
%Combines the SHEAR terms into one function
for s = 1:terms
if vB(s) == L
continue;
end
if vC(s) > 0
shear = vA(s)*(x.^(vC(s)).*heaviside(x-vB(s)) ) ;
elseif vC(s) == 0
shear = @(x) vA(s).*heaviside(x-vB(s));
elseif vC(s)< 0
shear = 0;
end
shearTotal = shearTotal + shear;
end
end