Hi guys,(adsbygoogle = window.adsbygoogle || []).push({});

I am the begginer of using the mathematica and now i have a problem how to write matlab code in mathematica: Actually I did but it does not work, Below I sent my matlab and mathematica code. Thanks for helping me.

MATLAB CODE:

function main_solver()

gamma = 0.98E12;

% Time range

time_start = 0; % Seconds

time_end = 100E-9; % Seconds % suggestion: 100E-9;

time_num_steps = 100000;

% Time step [secons]

time_step = abs(time_end-time_start)/time_num_steps;

% Normalized time step [a.u.]

h = time_step * gamma;

% Modulation parameters

J_freq = 100E6; % Hz

J_ampl = 0.1; % a.u.

J_avg = 0.25; % a.u.

% Period of the modulated current

J_period = 1/J_freq; % Seconds

% Number of time steps in a period

J_num_steps_period = round(J_period / time_step);

% Array declaration

T = double(zeros(time_num_steps,1));

J = double(zeros(time_num_steps,1));

state = 0;

count_steps_period = 0;

for idx = 1 : time_num_steps

count_steps_period = count_steps_period + 1;

if ( count_steps_period > J_num_steps_period )

if ( state == 0 )

state = 1;

else

state = 0;

end

count_steps_period = 1;

end

if ( state == 0 )

J(idx) = J_avg + J_ampl / 2;

else

J(idx) = J_avg - J_ampl / 2;

end

end

T(1) = time_start;

for idx = 2: time_num_steps

J(idx);

T(idx) = T(idx-1) + time_step;

end

plot(T, J, '.g')

title('Injected current')

end

MATHEMATICA CODE:

1)

freq = 10^8; (*Hz*)

amp = 0.1;

avg = 0.25;

per = 1/freq; (*period of modulated current*)

t1 = 100/10^9; (**)

t0 = 0;

number = 100000; (*number_steps*)

time = Abs[t1 - t0]/number; (*time step *)

step = Round[per/time];(*number of time steps in a period*)

flag = 0;

count = 0;

For[i = 1, i = number, i++,

count = count + 1;

{If[count > step,

If[flag == 0, flag = 1, flag = 0 ]],

count = 1,

If[flag == 0, p= avg + amp/2, p= avg - amp/2]}

]

For[i = 2, i = number, i++,

p&& T= T[i - 1] + time]

2) I tried to write as a function or subroutine

current[number_] :=

Module[{freq, amp, avg, per, t1, t0, time, step, flag, count, i, p,

T},

freq = 10^8; (*[Hz]*)

amp = 0.1;

avg = 0.25;

per = 1/freq; (*period of modulated current*)

t1 = 100/10^9; (**)

t0 = 0;

(*number=100000; number_steps*)

time = Abs[t1 - t0]/number; (*time step *)

step = Round[per/time]; (*number of time steps in a period*)

flag = 0;

count = 0;

For[i = 1, i = number, i++,

count = count + 1

{If[count > step,

If[flag == 0, flag = 1, flag = 0 ]]

count = 1

If[flag == 0, p= avg + amp/2, p= avg - amp/2]}

Return[Table[p, {i, number}]]]];

current[100000]

**Physics Forums - The Fusion of Science and Community**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# How to write matlab code in mathematica

Can you offer guidance or do you also need help?

Draft saved
Draft deleted

Loading...

Similar Threads - write matlab code | Date |
---|---|

How would you write this equation in MATLAB? (challenging!) | Apr 25, 2014 |

Writing mathematical equations in MATLAB | Nov 23, 2012 |

How to write factorial in matlab | Mar 3, 2012 |

Help with writing a matlab code | Feb 4, 2012 |

How to write this code in matlab? | Jan 31, 2012 |

**Physics Forums - The Fusion of Science and Community**