translating from python to matlab

1 回表示 (過去 30 日間)
Masky
Masky 2016 年 4 月 27 日
回答済み: Kevin 2016 年 4 月 29 日
Hi,
I'm having some difficulties translate this script in python to matlab. Could someone help me and give me a proposition?
endfrom matplotlib.pylab import *
N = 1000
r = linspace(0,10,N)
v = zeros(N)
v[0] = 0
a = 4
dr = r[1] - r[0]
c = 1
def cr(r):
if r <= a:
return c
else:
return 0
def vanalytic(r):
if r <= a:
return 0.5*c*r
else:
return 0.5*(c*a**2)/r
cr = vectorize(cr)
vanalytic = vectorize(vanalytic)
for i in range(1,N):
v[i] = ((r[i]*cr(r[i]) + r[i-1]*cr(r[i]))/2.)*(dr/r[i]) + (r[i-1]*v[i-1])/r[i]
plot(r,v,r,vanalytic(r))
legend(['Numerical','Analytical'],loc='best')
xlabel('r distance')
ylabel('v distance per seconds')
title('Oppgave c): R=10, c=1, a=4, N=1000')
show()
% code

回答 (1 件)

Kevin
Kevin 2016 年 4 月 29 日
I am not sure if this helps. But starting in R2016a (or maybe even in R2015b), you can call python function from MATLAB. So you don't need to re-code.

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by