the error im getting back is from the lines below comment "Combines the SHEAR terms into one function".
Arrays of Anonymous Functions used in for loop
1 view (last 30 days)
Show older comments
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'
4 Comments
Accepted Answer
Matt J
on 7 Mar 2020
Edited: Matt J
on 7 Mar 2020
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
3 Comments
Matt J
on 7 Mar 2020
You're welcome, but please Accept-click the answer to indicate that it resolved the issue.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!