How to write this thetta fuction in Matlab? I have an example code in Python

1 vue (au cours des 30 derniers jours)
How to write this thetta fuction in Matlab?
Someone wrote it in Python like this:
def thetta(r):
thettar = np.zeros(r.size)
h = (r_2 - r_1) / N
for i in range(r.size):
for j in range(i):
thettar[i] += h * 0.5 * (1 / (r[j] * math.tan(betta(r[j]))) + 1 / (r[j+1] * math.tan(betta(r[j+1]))))
return thettar
  10 commentaires
Dyuman Joshi
Dyuman Joshi le 26 Jan 2023
You can attach it here, use the paperclip icon.
Or you can copy paste releveant code.
Beket
Beket le 26 Jan 2023
@Dyuman Joshi but I'm afraid about security, because I really need this code

Connectez-vous pour commenter.

Réponse acceptée

Askic V
Askic V le 26 Jan 2023
Modifié(e) : Askic V le 26 Jan 2023
Ok, without going into too much details, let's say you have the following Python functions that you need to rewrite in Matlab.
Python code:
import math
import numpy as np
def thetta(r, N):
thettar = np.zeros(r.size)
r_1 = r[0]
r_2 = r[-1]
h = (r_2 - r_1) / N
for i in range(r.size):
for j in range(i):
thettar[i] += h * 0.5 * (1 / (r[j] * math.tan(betta(r[j]))) + 1 / (r[j+1] * math.tan(betta(r[j+1]))))
return thettar
def betta(x):
return 2*x
% Test results
N = 10
r_1 = 1
r_2 = 5
h = h = (r_2 - r_1) / N
r = np.arange(r_1 ,r_2 +h,h)
y = thetta(r, N)
print(y)
The Python code will produce teh following result:
[ 0. -0.4933463 -0.66999674 -0.41547207 -0.42690584 -0.69679015
-0.82237975 -0.70521665 -0.7204894 -0.93958357 -1.06804844]
Now, the equivalent implementation in Matlab would be:
clear
clc
r_1 = 1;
r_2 = 5;
N = 10;
h = (r_2 - r_1) / N;
% betta = @(x) 2*x; you can use function handle instead of full function def
r = r_1:h:r_2; % to have the same number of elements compared to python
thettar = zeros(size(r));
for i = 1: size(r,2)-1
for j = 1:i
thettar(i+1) = thettar(i+1) + h * 0.5 * (1 / (r(j) * tan(betta(r(j)))) + 1 / (r(j+1) * tan(betta(r(j+1)))));
end
end
thettar
%Some simple betta function, just to be used for test
function y = betta(x)
y = 2*x;
end
and this code will produce the result:
thettar =
0 -0.4933 -0.6700 -0.4155 -0.4269 -0.6968
-0.8224 -0.7052 -0.7205 -0.9396 -1.0680
So, basically that is all you need. The biggest hassle is to keep in mind that index strats with 0 in Python and with 1 in Matlab.
  5 commentaires
Beket
Beket le 26 Jan 2023
@Askic V I wrote how to define Q_rk in line 25
Askic V
Askic V le 26 Jan 2023
Modifié(e) : Askic V le 26 Jan 2023
You did, but it is not a global variable, and it is not visible inside the function:
function c_rinf = c_rinf(r)
c_rinf = Q_rk / (2 * pi * r * b(r));
end
so you need to send it as an input argument, for example:
function c_rinf = c_rinf(Q_rk, r)
c_rinf = Q_rk / (2 * pi * r * b(r));
end
You need to pay attention to this type of things.
Next you use a lot of bad practice, for example, variable name is the same as function etc...

Connectez-vous pour commenter.

Plus de réponses (1)

Dyuman Joshi
Dyuman Joshi le 26 Jan 2023
@Beket, your code needs some changes
94th and 95th line of your code, you used b in c_rinf before defining it.
c_rinf = @(r) Q_rk ./ (2 .* pi .* r .* b(r));
b = @(r) b_1 - (b_1 - b_2) ./ (r_2 - r_1) .* (r - r_1);
Similarly, in lines 110th and 111th, you used r in thettar before defining it.
thettar = zeros(size(r));
r = linspace(r_1, r_2, N);
Correct the order and the error will be rectified.
Also, while defining a function handle, it's better to use element-wise operators .*, ./ and .^ , you can see that change above in c_rinf and b.
Your code still has some errors, mostly syntax errors (check line 121 and 150).
Additionally, what is the purpose of these lines?
bettar = betta(r);
thettar = thettar(r);
  4 commentaires
Beket
Beket le 26 Jan 2023
@Dyuman Joshi No, I thought you could fix specific places
Dyuman Joshi
Dyuman Joshi le 26 Jan 2023
Did you make the changes I suggested in my answer and run the code?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by