Convert maple code to MATLAB code
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello, I am having trouble translating Maple code to MATLAB code.
The code I am trying to translate is:
h:= 0.1;
f:= (x,y) -> 1+x^2+y;
x:= 0; y:= 1;
ans:= [x,y];
while x<1
do
F:=f(x,y); x:= x+h; y:= y+h*F;
od;
print(y); ans:= ans,[x,y];
댓글 수: 0
채택된 답변
Walter Roberson
2019년 5월 2일
편집: Walter Roberson
2019년 5월 2일
In MATLAB, due to finite precision, if you start with 0.1 and add 0.1 nine times, the result is slightly less than 1.
In Maple, 0.1 is represented as an sfloat, a software float, which uses a base 10 representation and so is able to represent 0.1 as exactly 1 * 10^(-1) . Adding 9 copies of that to the initial 0.1 gives you exactly 1 as a result.
Therefore the MATLAB version that madhan ravi suggests gives one more iteration than the Maple version does.
I have enclosed the translation of the Maple code to MATLAB code. There are, however, a few small differences in formatting, and I took the short-cut of implementing x^2 as x*x
It would have been a lot simpler and faster to make a minor modification to the algorithm to not add up 0.1's instead of having to implement decimal arithmetic in order to do exactly the same thing that Maple does.
댓글 수: 0
추가 답변 (1개)
madhan ravi
2019년 5월 2일
More or less:
h= 0.1;
f= @(x,y) 1+x.^2+y;
x= 0;
y= 1;
r = [x,y];
while x<1
F=f(x,y);
x= x+h;
y= y+h*F;
end
disp(y);
R=[x,y];
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!