Exporting a code from Maple to Matlab
이전 댓글 표시
I use Matlab for numerical calculations.
But I use Maple for symbolic calculations and after the calculations I usually get piecewise functions in my maple codes.
I think the 3d plots in Matlab is very attractive more than Maple plots. So, I want to export piecewise functions in maple to a Matlab code in order to utilize advantages of plots of Matlab.
MAPLE CODE
restart:
u:=1/(1. + exp(x))^2 + 1/(1. + exp(-5.*t))^2 - 0.2500000000 + x*(1/(1. + exp(1 - 5*t))^2 - 1./((1. + exp(-5*t))^2) + 0.1776705118 + 0.0415431679756514*piecewise(0. <= t and t <= 0.5000000000, 1.732050808, 0.) + 0.00922094377856479*piecewise(0. <= t and t <= 0.5000000000, 30.98386677*t - 7.745966692, 0.) + 0.0603742508215732*piecewise(0.5000000000 <= t and t <= 1., 1.732050808, 0.) - 0.00399645630498528*piecewise(0.5000000000 <= t and t <= 1., 30.98386677*t - 23.23790008, 0.)) + (-0.00243051684581302*piecewise(0. <= x and x <= 0.5000000000, 1.732050808, 0.) - 0.000809061198761621*piecewise(0. <= x and x <= 0.5000000000, 30.98386677*x - 7.745966692, 0.) - 0.0152377552205917*piecewise(0.5000000000 <= x and x <= 1., 1.732050808, 0.) - 0.00195593427342862*piecewise(0.5000000000 <= x and x <= 1., 30.98386677*x - 23.23790008, 0.))*piecewise(0. <= t and t <= 0.5000000000, 1.732050808, 0.) + (-0.000433590063316381*piecewise(0. <= x and x <= 0.5000000000, 1.732050808, 0.) - 0.000146112803263678*piecewise(0. <= x and x <= 0.5000000000, 30.98386677*x - 7.745966692, 0.) - 0.00319022339097685*piecewise(0.5000000000 <= x and x <= 1., 1.732050808, 0.) - 0.000477063086307787*piecewise(0.5000000000 <= x and x <= 1., 30.98386677*x - 23.23790008, 0.))*piecewise(0. <= t and t <= 0.5000000000, 30.98386677*t - 7.745966692, 0.) + (-0.00276114805649180*piecewise(0. <= x and x <= 0.5000000000, 1.732050808, 0.) - 0.000933166016624500*piecewise(0. <= x and x <= 0.5000000000, 30.98386677*x - 7.745966692, 0.) - 0.0207984584912892*piecewise(0.5000000000 <= x and x <= 1., 1.732050808, 0.) - 0.00314360556336114*piecewise(0.5000000000 <= x and x <= 1., 30.98386677*x - 23.23790008, 0.))*piecewise(0.5000000000 <= t and t <= 1., 1.732050808, 0.) + (0.000172746997599710*piecewise(0. <= x and x <= 0.5000000000, 1.732050808, 0.) + 0.0000586775450031145*piecewise(0. <= x and x <= 0.5000000000, 30.98386677*x - 7.745966692, 0.) + 0.00136190009033518*piecewise(0.5000000000 <= x and x <= 1., 1.732050808, 0.) + 0.000211410172315387*piecewise(0.5000000000 <= x and x <= 1., 30.98386677*x - 23.23790008, 0.))*piecewise(0.5000000000 <= t and t <= 1., 30.98386677*t - 23.23790008, 0.):
>
plot3d( u,
x=0..1,
t=0..1,
style=surface,
axes=boxed,
colorscheme=[yellow, red]
);
We can transform a maple code to Matlab code by using
with(CodeGeneration):
Matlab(u,resultname="w");
. But The code can' t properly transform to Matlab code.
Could you help me pls
댓글 수: 2
Rik
2020년 10월 16일
Is your question how to implement this code in Matlab, or how to successfully convert the code in Maple? As it stands now your question seems better suited to a Maple forum.
student_md
2020년 10월 16일
채택된 답변
추가 답변 (2개)
KSSV
2020년 10월 16일
Some thing like this:
x = linspace(0,1) ;
t = linspace(0,1) ;
[x,t] = meshgrid(x,t) ;
u =1./(1. + exp(x)).^2 + 1./(1. + exp(-5.*t)).^2 - 0.2500000000 + x.*(1./(1. + exp(1 - 5*t)).^2 - 1./((1. + exp(-5*t)).^2) .......
+ 0.1776705118 + 0.0415431679756514*piecewise(0. <= t && t <= 0.5000000000, 1.732050808, 0.) .......
+ 0.00922094377856479*piecewise(0. <= t && t <= 0.5000000000, 30.98386677*t - 7.745966692, 0.) .........
+ 0.0603742508215732*piecewise(0.5000000000 <= t && t <= 1., 1.732050808, 0.) ..........
- 0.00399645630498528*piecewise(0.5000000000 <= t && t <= 1., 30.98386677*t ........
- 23.23790008, 0.)) + (-0.00243051684581302*piecewise(0. <= x && x <= 0.5000000000, 1.732050808, 0.).........
- 0.000809061198761621*piecewise(0. <= x && x <= 0.5000000000, 30.98386677*.x .........
- 7.745966692, 0.) - 0.0152377552205917*piecewise(0.5000000000 <= x && x <= 1., 1.732050808, 0.) ........
- 0.00195593427342862*piecewise(0.5000000000 <= x && x <= 1., 30.98386677*x - 23.23790008, 0.)).*piecewise(0. <= t && t <= 0.5000000000, 1.732050808, 0.) ........
+ (-0.000433590063316381*piecewise(0. <= x && x <= 0.5000000000, 1.732050808, 0.) .........
- 0.000146112803263678*piecewise(0. <= x && x <= 0.5000000000, 30.98386677*x - 7.745966692, 0.) .........
- 0.00319022339097685*piecewise(0.5000000000 <= x && x <= 1., 1.732050808, 0.) ...........
- 0.000477063086307787*piecewise(0.5000000000 <= x && x <= 1., 30.98386677*x - 23.23790008, 0.)).*piecewise(0. <= t && t <= 0.5000000000, 30.98386677*t - 7.745966692, 0.) .........
+ (-0.00276114805649180*piecewise(0. <= x && x <= 0.5000000000, 1.732050808, 0.) .......
- 0.000933166016624500*piecewise(0. <= x && x <= 0.5000000000, 30.98386677*x - 7.745966692, 0.)...............
- 0.0207984584912892*piecewise(0.5000000000 <= x && x <= 1., 1.732050808, 0.) .............
- 0.00314360556336114*piecewise(0.5000000000 <= x && x <= 1., 30.98386677*x - 23.23790008, 0.)).*piecewise(0.5000000000 <= t && t <= 1., 1.732050808, 0.) ..............
+ (0.000172746997599710*piecewise(0. <= x && x <= 0.5000000000, 1.732050808, 0.) + 0.0000586775450031145*piecewise(0. <= x && x <= 0.5000000000, 30.98386677*x - 7.745966692, 0.) .............
+ 0.00136190009033518*piecewise(0.5000000000 <= x && x <= 1., 1.732050808, 0.) .......
+ 0.000211410172315387*piecewise(0.5000000000 <= x && x <= 1., 30.98386677*x - 23.23790008, 0.)).*piecewise(0.5000000000 <= t && t <= 1., 30.98386677*t - 23.23790008, 0.) ;
surf(x,t,u)
shading interp
colorbar
If error throws, may be you have to use element by element multiplication .*.
or use element by element divison, ./
Replace and with &&
Or Repalce all && with &.
댓글 수: 3
student_md
2020년 10월 16일
편집: student_md
2020년 10월 16일
Walter Roberson
2020년 10월 16일
piecewise() is defined, but it requires the Symbolic toolbox.
Walter Roberson
2024년 10월 6일
function result = piecewise(condition, truevalue, falsevalue)
if numel(truevalue) == 1
result = repmat(truevalue, size(condition))
else
result = truevalue;
end
if numel(falsevalue) == 1
result(~condition) = falsevalue;
else
result(~condition) = falsevalue(~condition);
end
end
or something similar that accounts for the possibility that the true or false conditions might be expressed as scalars.
There is the possibility that the condition might be a scalar but the truevalue or falsevalue might be non-scalar. In such a case, the result of the piecewise() should be the entire non-scalar truevalue or falsevalue; the above code does not work properly for this case.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!