could you please translate the following code for me?

조회 수: 4 (최근 30일)
B
B 2015년 3월 11일
댓글: B 2015년 3월 14일
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
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!
  댓글 수: 4
B
B 2015년 3월 11일
Thank you very much (Y)
B
B 2015년 3월 14일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by