could you please translate the following code for me?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Greig
2015-3-11
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!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!