How to write this thetta fuction in Matlab? I have an example code in Python
2 次查看(过去 30 天)
显示 更早的评论
How to write this thetta fuction in Matlab?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1274695/image.png)
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 个评论
Dyuman Joshi
2023-1-26
You can attach it here, use the paperclip icon.
Or you can copy paste releveant code.
采纳的回答
Askic V
2023-1-26
编辑:Askic V
2023-1-26
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 个评论
Askic V
2023-1-26
编辑:Askic V
2023-1-26
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...
更多回答(1 个)
Dyuman Joshi
2023-1-26
@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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!