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

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 Comments

Can you please show us your current attempt?
@Askic V but I think this is the wrong way(
thettar = zeros(size(r));
h = (r_2 - r_1) / N;
for i = 1:length(r)
for j = 1:i-1
thettar(i) = thettar(i) + h * 0.5 * (1 / (r(j) * tan(betta(r(j)))) + 1 / (r(j+1) * tan(betta(r(j+1)))));
end
end
How is betta defined? You should be able to execute the same code in Python and Matlab and compare results.
Matlab has some very good built in functions for integration. For numerical integration I use cumtrapz, but I guess your task is to rewrite the Python implementation to Matlab.
@Askic V Yes, I have to rewrite the Python implementation to Matlab. I have no idea how to write this integral. Because it shows that Conversion to function_handle from double is not possible.
Are you getting an error? If so, please mention the full error.
And/Or are you not getting the output you want? In that case, attach or provide the full code and data for r (a sample data would work as well) and the corresponding required output.
betta = @(r) asin(c_rinf(r) / w(r) + z * s / (2 * pi * r));
N = 100;
thettar = @(r) zeros(size(r));
r = linspace(r_1, r_2, N);
h = (r_2 - r_1) / N;
for i = 1:length(r)
for j = 1:i-1
thettar(i) = thettar(i) + (h * 0.5 * ((1 / (r(j) * tan(betta(r(j))))) + (1 / (r(j+1) * tan(betta(r(j+1)))))));
end
end
thettar;
Error:
Conversion to function_handle from double is not possible.
Error in characteristics_impeller (line 115)
thettar(i) = thettar(i) + (h * 0.5 * ((1 / (r(j) * tan(betta(r(j))))) + (1 / (r(j+1) * tan(betta(r(j+1)))))));
What are c_rinf, w, s and z in the definition of betta?
Are they defined before their use in betta?
@Dyuman Joshi Can I sent my Matlab code and Python code to you in direct? Can you help me with this code?
You can attach it here, use the paperclip icon.
Or you can copy paste releveant code.
@Dyuman Joshi but I'm afraid about security, because I really need this code

Sign in to comment.

 Accepted Answer

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 Comments

thank a lot! I think I make sure this issue, but I have another problem in code. I should send you all code. Error: Error: File: Copy_of_characteristics_impeller.m Line: 89 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "c_rinf" function definition to before the first local function definition.
First of all, all function definitions should go at the end of the script or to save each function in the separate file 'function_name.m' (name of the file must be the same as the name of the function inside it).
Second, you use undefined variables, for example: Q_rk, how Matlab would know in the line:
c_rinf = Q_rk / (2 * pi * r * b(r));
what Q_rk is?
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...

Sign in to comment.

More Answers (1)

@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 Comments

I already have your original code. Is this one different from that one?
@Dyuman Joshi No, I thought you could fix specific places
Did you make the changes I suggested in my answer and run the code?

Sign in to comment.

Products

Release

R2022a

Asked:

on 26 Jan 2023

Commented:

on 26 Jan 2023

Community Treasure Hunt

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

Start Hunting!