How to convert Maple Code to Matlab code

7 ビュー (過去 30 日間)
Brendan O'Connor
Brendan O'Connor 2017 年 1 月 4 日
コメント済み: Stephen23 2017 年 4 月 10 日
My class partner did our homework in Maple code, but to submit it we need it to work in Matlab. She did this when she was in her home country for the holidays and doesn't have access to Maple anymore (and neither do I), but I have read that there is a built in function in Maple that can do it automatically. Can someone convert this for me or at least show me how? I have no experience at all with Maple.
restart: with(LinearAlgebra)
n:=5; A:=Matrix(n,n); B:=Vector(n,[1.0,1.5,1.0,1.0,1.0]); BB:=Vector(n,[1.0,1.5,1.0,1.0,1.0]);
for i to n do
for j to n do
if j=i then A[i,j]:=2; end if;
if i+1=j or i-1=j then A[i,j]:=-1 end if;
end do;
end do;
print(A);
RR:=LinearSolve(A,BB,method='Cholesky', inplace); A; B;
print(A); print(B);
CONJUGENT GRADIENT METHOD:
niter:=5:
X:=Vector(n); R:=Vector(n);R1:=Vector(n);R2:=Vector(n); W:=Vector(n); P:=Vector(n);
for i to n do X[i]:=0.0: R1[i]:=B[i];end do;
GR1:=NULL:GR2:=NULL;
for k to niter do
if k=1 then for i to n do P[i]:=R1[i]: end do;
else
restart: with(LinearAlgebra):
n:=5; A:=Matrix(n,n); B:=Vector(n,[1.0,1.5,1.0,1.0,1.0]); BB:=Vector(n,[1.0,1.5,1.0,1.0,1.0]);
for i to n do
for j to n do
if j=i then A[i,j]:=2; end if;
if i+1=j or i-1=j then A[i,j]:=-1 end if;
end do;
end do;
print(A);
RR:=LinearSolve(A,BB,method='Cholesky', inplace); A; B;
print(A); print(B);
CONJUGENT GRADIENT METHOD:
niter:=5:
X:=Vector(n); R:=Vector(n);R1:=Vector(n);R2:=Vector(n); W:=Vector(n); P:=Vector(n);
for i to n do X[i]:=0.0: R1[i]:=B[i];end do;
GR1:=NULL:GR2:=NULL;
for k to niter do
if k=1 then for i to n do P[i]:=R1[i]: end do;
else
beta:=add(R1[j]^2,j=1..n)/add(R2[j]^2,j=1..n);
for i to n do P[i]:=R1[i]+beta*P1[i]: end do;
end if;
for i to n do W[i]:=add(A[i,j]*P[j],j=1..n); end do;
vard:=add(P[j]*W[j],j=1..n); printf("k=%d vard=%e\n",k,vard);
alfa:=add(R1[j]^2,j=1..n)/vard;
for i to n do X[i]:=X[i]+alfa*P[i]: end do;
for i to n do R[i]:=R1[i]-alfa*add(A[i,j]*P[j],j=1..n): R2[i]:=R1[i]; R1[i]:=R[i]; P1[i]:=P[i]; end do;
eps:=sqrt(add(R[j]*R[j],j=1..n)); printf("k=%d eps=%e\n",k,eps); print(X);
for i to n do W[i]:=add(A[i,j]*X[j],j=1..n):end do: func:=add(X[j]*W[j],j=1..n)-add(B[j]*X[j],j=1..n);
GR1:=GR1,[k,func]; GR2:=GR2,[k,eps]; printf("k=%d func=%e eps=%e\n",k,func, eps); print(A); print(B);
end do;
RR; X;
plot([GR1],axes=boxed); plot([GR2],axes=boxed);
beta:=add(R1[j]^2,j=1..n)/add(R2[j]^2,j=1..n);
for i to n do P[i]:=R1[i]+beta*P1[i]: end do;
end if;
for i to n do W[i]:=add(A[i,j]*P[j],j=1..n); end do;
vard:=add(P[j]*W[j],j=1..n); printf("k=%d vard=%e\n",k,vard);
alfa:=add(R1[j]^2,j=1..n)/vard;
for i to n do X[i]:=X[i]+alfa*P[i]: end do;
for i to n do R[i]:=R1[i]-alfa*add(A[i,j]*P[j],j=1..n): R2[i]:=R1[i]; R1[i]:=R[i]; P1[i]:=P[i]; end do;
eps:=sqrt(add(R[j]*R[j],j=1..n)); printf("k=%d eps=%e\n",k,eps); print(X);
for i to n do W[i]:=add(A[i,j]*X[j],j=1..n):end do: func:=add(X[j]*W[j],j=1..n)-add(B[j]*X[j],j=1..n);
GR1:=GR1,[k,func]; GR2:=GR2,[k,eps]; printf("k=%d func=%e eps=%e\n",k,func, eps); print(A); print(B);
end do;
RR; X;
plot([GR1],axes=boxed); plot([GR2],axes=boxed);

採用された回答

Walter Roberson
Walter Roberson 2017 年 1 月 4 日
The conversion tool in Maple, CodeGeneration[Matlab], cannot convert calls like LinSolve. And the parts it can convert of your code are not difficult to convert by hand. The conversion tool does a poor job formatting.
(I have a revised version that does better formatting but I have not been able to track down some significant bugs in what I wrote.)
  4 件のコメント
Walter Roberson
Walter Roberson 2017 年 4 月 10 日
I have no interest in typing in code from an image.
Stephen23
Stephen23 2017 年 4 月 10 日
@Azamat Durzhanbayev: that is a nice screenshot, but what do you want us to do with it? It might look nice printed out and stuck on the wall.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by