Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
converting mathematica code to matlab

Subject: converting mathematica code to matlab

From: Paulo

Date: 27 Nov, 2010 02:23:03

Message: 1 of 6

Hi, I'm trying to make something similar to http://demonstrations.wolfram.com/PendulumOnAMovingSupport/ , I already have a function to do the animation given the position of the cart and angle of pendulum but I just don't know how to do the solving of those differential equations in matlab, can someone please help me? thank you in advance

Subject: converting mathematica code to matlab

From: Nasser M. Abbasi

Date: 27 Nov, 2010 20:56:10

Message: 2 of 6

On Nov 26, 6:23 pm, "Paulo " <paulojmdsi...@gmail.com> wrote:
> Hi, I'm trying to make something similar tohttp://demonstrations.wolfram.com/PendulumOnAMovingSupport/, I already have a function to do the animation given the position of the cart and angle of pendulum but I just don't know how to do the solving of those differential equations in matlab, can someone please help me? thank you in advance


did you try 'dsolve' ?

"EDU>> help dsolve
 DSOLVE Symbolic solution of ordinary differential equations.
    DSOLVE('eqn1','eqn2', ...) accepts symbolic equations representing
    ordinary differential equations and initial conditions."

--Nasser

Subject: converting mathematica code to matlab

From: Paulo

Date: 27 Nov, 2010 23:00:06

Message: 3 of 6

"Nasser M. Abbasi" <nma@12000.org> wrote in message <d15de460-70e9-4d4a-99a5-99a6335248ec@i10g2000prd.googlegroups.com>...
> On Nov 26, 6:23 pm, "Paulo " <paulojmdsi...@gmail.com> wrote:
> > Hi, I'm trying to make something similar tohttp://demonstrations.wolfram.com/PendulumOnAMovingSupport/, I already have a function to do the animation given the position of the cart and angle of pendulum but I just don't know how to do the solving of those differential equations in matlab, can someone please help me? thank you in advance
>
>
> did you try 'dsolve' ?
>
> "EDU>> help dsolve
> DSOLVE Symbolic solution of ordinary differential equations.
> DSOLVE('eqn1','eqn2', ...) accepts symbolic equations representing
> ordinary differential equations and initial conditions."
>
> --Nasser

Hi thanks for the reply, I tried like this:

clc;syms a;syms x;l=1;t=0:0.1:1;
eq='D2a(t)+D2x(t)/l*cos(a(t))+9.8/l*sin(a(t))=0,2*D2x(t)+l*D2a(t)*cos(a(t))-l*(Da)^2*sin(a(t))=0)';
dsolve(eq,'x(0)=0','a(0)=0','Dx(0)=0','Da(0)=0')

but it doesn't work, error:

??? Attempted to access Eqn(1); index out of bounds because numel(Eqn)=0.

Error in ==> dsolve at 240
if isequal(Eqn(1),'[') & isequal(Eqn(end),']')

Error in ==> testeDSOLVE at 3
dsolve(eq,'x(0)=0','a(0)=0','Dx(0)=0','Da(0)=0')

I don't know what to do in order to make it work, any ideas?

Subject: converting mathematica code to matlab

From: Paulo

Date: 27 Nov, 2010 23:07:14

Message: 4 of 6

I had a mistake on the previous code but after the correction the error remains

clc;syms a;syms x;l=1;t=0:0.1:1;
eq1='D2a(t)+D2x(t)/l*cos(a(t))+9.8/l*sin(a(t))=0';
eq2='2*D2x(t)+l*D2a(t)*cos(a(t))-l*(Da)^2*sin(a(t))=0';
dsolve(eq1,eq2,'x(0)=0','a(0)=0','Dx(0)=0','Da(0)=0')

Subject: converting mathematica code to matlab

From: Paulo

Date: 28 Nov, 2010 00:17:03

Message: 5 of 6

I found some errors, this version works but I didn't check if the result is correct yet

clc;l=1;
eq1='D2a+D2x/l*cos(a)+9.8/l*sin(a)=0';
eq2='2*D2x+l*D2a*cos(a)-l*(Da)^2*sin(a)=0';
[angulo,posicao]=dsolve(eq1,eq2,'x(0)=0','a(0)=0','Dx(0)=0','Da(0)=0','t')

Subject: converting mathematica code to matlab

From: Jan Winkler

Date: 26 Apr, 2012 07:01:28

Message: 6 of 6

"Paulo Silva" wrote in message <icpq27$evn$1@fred.mathworks.com>...
> Hi, I'm trying to make something similar to http://demonstrations.wolfram.com/PendulumOnAMovingSupport/ , I already have a function to do the animation given the position of the cart and angle of pendulum but I just don't know how to do the solving of those differential equations in matlab, can someone please help me? thank you in advance

Hey there,

since you already have the equations of motion in Mathematica, why not do the solving there and neatly convert it to MATLAB M-code? There's a Mac-tool called "Converso" which does exactly that (and more - generating C-code for example). Its still in beta, but already fully functional for common equation cases.

You can get it from the Facebook page: http://www.facebook.com/ConversoEngine

There's also an example covering a single pendulum: http://optinetics.net/Converso/Versions/0.92b/Pendulum_Example.zip

I know, this answer might be a bit late, but maybe it helps someone else.

Cheers,
Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us