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
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
student_md 2020년 10월 16일
How to implement this Maple code in Matlab?

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

 채택된 답변

Walter Roberson
Walter Roberson 2020년 10월 16일

1 개 추천

Optimized version of the calculation:
t1 = exp(x);
t21 = -5 .* t;
t3 = exp(t21);
t5 = exp((1 + t21));
t4 = ((0 <= t & t <= 1/2) .* 1.73205);
t5 = ((0 <= t & t <= 1/2) .* (30.9839 .* t - 7.74597));
t6 = ((1/2 <= t & t <= 1) .* 1.73205);
t7 = ((1/2 <= t & t <= 1) .* (30.9839 .* t - 23.2379));
t6 = (1 + t5).^2;
t2 = 1 ./ t6;
t7 = (1 + t3).^2;
t3 = 1 ./ t7;
t8 = -0.00399646;
t9 = 0.00922094;
t10 = 0.0415432;
t11 = 0.0603743;
t12 = 0.177671;
t13 = ((0 <= x & x <= 1/2) .* 1.73205);
t14 = ((0 <= x & x <= 1/2) .* (30.9839 .* x - 7.74597));
t15 = ((1/2 <= x & x <= 1) .* 1.73205);
t16 = ((1/2 <= x & x <= 1) .* (30.9839 .* x - 23.2379));
t8 = (1 + t1).^2;
t1 = 1 ./ t8;
u1 = -1/4 + (-0.00243052 .* t13 - 0.000809061 .* t14 - 0.00195593 .* t16 - 0.0152378 .* t15) .* t4 + (-0.00043359 .* t13 - 0.000146113 .* t14 - 0.000477063 .* t16 - 0.00319022 .* t15) .* t5 + (-0.00276115 .* t13 - 0.000933166 .* t14 - 0.00314361 .* t16 - 0.0207985 .* t15) .* t6 + t7 .* (0.000172747 .* t13 + 0.0013619 .* t15 + 0.00021141 .* t16 + 5.86775e-05 .* t14) + x .* (t10 .* t4 + t11 .* t6 + t5 .* t9 + t7 .* t8 + t12 + t2 - t3) + t3 + t1;
Unoptimized version of the calculation.
u1 = 1 ./ (1 + exp(x)).^2 + 1 ./ (1 + exp(-(5 .* t))).^2 - 1/4 + x .* (1 ./ (1 + exp((1 - 5 .* t))).^2 - 1 ./ (1 + exp(-(5 .* t))).^2 + 0.177671 + 0.0415432 .* ((0 <= t & t <= 1/2) .* 1.73205) + 0.00922094 .* ((0 <= t & t <= 1/2) .* (30.9839 .* t - 7.74597)) + 0.0603743 .* ((1/2 <= t & t <= 1) .* 1.73205) - 0.00399646 .* ((1/2 <= t & t <= 1) .* (30.9839 .* t - 23.2379))) + (-0.00243052 .* ((0 <= x & x <= 1/2) .* 1.73205) - 0.000809061 .* ((0 <= x & x <= 1/2) .* (30.9839 .* x - 7.74597)) - 0.0152378 .* ((1/2 <= x & x <= 1) .* 1.73205) - 0.00195593 .* ((1/2 <= x & x <= 1) .* (30.9839 .* x - 23.2379))) .* ((0 <= t & t <= 1/2) .* 1.73205) + (-0.00043359 .* ((0 <= x & x <= 1/2) .* 1.73205) - 0.000146113 .* ((0 <= x & x <= 1/2) .* (30.9839 .* x - 7.74597)) - 0.00319022 .* ((1/2 <= x & x <= 1) .* 1.73205) - 0.000477063 .* ((1/2 <= x & x <= 1) .* (30.9839 .* x - 23.2379))) .* ((0 <= t & t <= 1/2) .* (30.9839 .* t - 7.74597)) + (-0.00276115 .* ((0 <= x & x <= 1/2) .* 1.73205) - 0.000933166 .* ((0 <= x & x <= 1/2) .* (30.9839 .* x - 7.74597)) - 0.0207985 .* ((1/2 <= x & x <= 1) .* 1.73205) - 0.00314361 .* ((1/2 <= x & x <= 1) .* (30.9839 .* x - 23.2379))) .* ((1/2 <= t & t <= 1) .* 1.73205) + (0.000172747 .* ((0 <= x & x <= 1/2) .* 1.73205) + 5.86775e-05 .* ((0 <= x & x <= 1/2) .* (30.9839 .* x - 7.74597)) + 0.0013619 .* ((1/2 <= x & x <= 1) .* 1.73205) + 0.00021141 .* ((1/2 <= x & x <= 1) .* (30.9839 .* x - 23.2379))) .* ((1/2 <= t & t <= 1) .* (30.9839 .* t - 23.2379));
Before this you would do the kind of meshgrid() bit that KSSV indicated, and you could surf() the way he did as well.

댓글 수: 6

student_md
student_md 2020년 10월 17일
Thanks for your interest. This is ok for this piecewise function. But I am seeking to general code for transforming from Maple to Matlab. Because I usually use different and complicated piecewise functions in Maple.
I use Matlab for numerical calculation and I think the 3d plots in matlab is very attractive more than Maple plots. But I use Maple for symbolic calculations.
Walter Roberson
Walter Roberson 2020년 10월 17일
The Maple function you used does not know how to translate piecewise() and ends up generating matlab code that includes piecewise() calls. On the MATLAB end that would require the symbolic toolbox. To create a numeric routine, MATLAB's matlabFunction would be needed, but that can only generate code for piecewise if you are writing to a file (and the generated code is not vectorized.)
You might also have noticed that the MATLAB code generated by maple is rather ugly. And not always vectorized properly.
Years ago, I adapted maple's code generation to be able to create clearer matlab code. I got a bit stuck, though, with it failing on some expressions, and I never did figure out why.
When you posted your question, I went back to my maple code and improved it to be able to handle some piecewise() calls. I also improved my understanding of how the maple code works, and fixed some bugs in my old code and created better matlab code. I can see that it would be possible to do better, but I had to stop for the night.
The results that I posted here were generated from my maple code, except that I was working on a cleaned up version of your function, such as having replaced floating point 0.5000000 with 1/2.
The piecewise() that my code can now handle is the three parameter case where the first is a condition and the second is selected if the condition is true and the third is selected if the condition is false. Also it requires that the unselected branch does not return infinity or nan when evaluated at locations not intended to be covered.
Handling multiple conditions in the same piecewise() call is a fair bit trickier, as the additional conditions should not be selected unless the earlier ones fail.. I would need to research this more.
I do not think my code is ready for public release until I can get a better understanding of why it fails completely on some expressions, but I will try to have another look at that when I get some time. I will also think about automatically converting some floating point numbers to simple fractions.
student_md
student_md 2020년 10월 17일
What a good news Dear Walter Roberson.
We are all looking forward to your code :)
Many thanks.
student_md
student_md 2020년 12월 2일
Is there any progress in your Maple code for creating clearer matlab code?
Best regards.
Unfortunately I recently had a disk crash that affected the drive I was using and the automatic backups of it. I had copied files from it not long before that, but a bunch of the copied files are empty, and I fear that my entire progress on this topic might have vanished.
student_md
student_md 2020년 12월 3일
I would like to state that I am very sorry for the unfortunate crash you have experienced. I hope you can recover all of your data as soon as possible.
Me and a lot of people who need the fantastic Maple code are looking forward to good news from you :)
Best regards.

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

추가 답변 (2개)

KSSV
KSSV 2020년 10월 16일

0 개 추천

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
student_md 2020년 10월 16일
편집: student_md 2020년 10월 16일
The code is not working...I think your code for piecewise function is not appropriate for Matlab.
Walter Roberson
Walter Roberson 2020년 10월 16일
piecewise() is defined, but it requires the Symbolic toolbox.
If you take @KSSV's code, but change all of the && to & and then you define
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.

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

Haitham
Haitham 2024년 10월 6일

0 개 추천

Thanks everyone

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

2020년 10월 16일

댓글:

2024년 10월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by