# could you please translate the following code for me?

23 views (last 30 days)
B on 11 Mar 2015
Commented: B on 14 Mar 2015
Hello, I'm copying a certain code from a lecture on singularity functions.. Could anyone explain the steps of the following code for me? I'm a beginner in MATLAB :( i got the following questions?? why to use a linspace? why to use a loop? Is it because we are dealing with a vector? Why is he chaning the variable from x-->xx-->> xxx
This is highly appreciated.. ---------------------------------------------------------------------------
function beam(x)
xx = linspace(0,x);
n=length(xx);
for i=1:n
uy(i) = -5/6.*(sing(xx(i),0,4)-sing(xx(i),5,4));
uy(i) = uy(i) + 15/6.*sing(xx(i),8,3) + 75*sing(xx(i),7,2);
uy(i) = uy(i) + 57/6.*xx(i)^3 - 238.25.*xx(i);
end
plot(xx,uy)
function s = sing(xxx,a,n)
if xxx > a
s = (xxx - a).^n;
else
s=0;
end

Greig on 11 Mar 2015
I may not know the purpose of the function, but I can answer your questions...
xx = linspace(0,x);
This line generates 100 equally spaced points between 0 and x. If you change it to linspace(0, x, N), this will generate N equally spaced points between 0 and x.
The loop here is needed because uy and xx are vectors, but the function sing is written in a way that it will only do what you want if the input is a scalar. It isn't actually needed, the code can be written without the loop, which will speed things up a little...
function My_beam(x)
xx = linspace(0,x);
uy = (-5/6).*(sing(xx,0,4)-sing(xx,5,4))...
+ (15/6).*sing(xx,8,3) + 75*sing(xx,7,2)...
+ (57/6).*xx.^3 - 238.25.*xx;
plot(xx,uy)
function s = sing(xxx,a,n)
Num = length(xxx);
s=zeros(1,Num);
s(xxx>a) = (xxx(xxx>a) - a).^n;
As for why the writer changes from x, to xx, to, xxx, well that is anybody's guess. He will have been trying to use distinct variables for the different parts of the code. It is always better to try and name variables according to what they are.
Hope this helps!
B on 14 Mar 2015
I have one more question on the code that you've provided. Why do we need the following two lines?
Num = length(xxx); s=zeros(1,Num);