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

1 visualizzazione (ultimi 30 giorni)
Beket il 25 Gen 2023
Risposto: Luca Ferro il 25 Gen 2023
How to write this fuction in Matlab?
Someone wrote it in Python like this:
def b(r):
return b_1 - (b_1 - b_2) / (r_2 - r_1) * (r - r_1)
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Jan il 25 Gen 2023
Assuming that b_1, b_2, r_1, r_2 are constants:
% As anonymous function:
r_1 = rand; % Define the constants
r_2 = rand;
b_1 = rand;
b_2 = rand;
b = @(r) b_1 - (b_1 - b_2) / (r_2 - r_1) * (r - r_1);
% Or as a function:
function out = b(r)
r_1 = rand; % Define the constants
r_2 = rand;
b_1 = rand;
b_2 = rand;
out = b_1 - (b_1 - b_2) / (r_2 - r_1) * (r - r_1);
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (2)

Jiri Hajek il 25 Gen 2023
Hi, your function contains four parameters, so the python code is incomplete. You can do this several ways in MATLAB, classic syntax would be:
% Test call
myVar = bFunName(10)
myVar = -5
% function definition
function b = bFunName(r)
b_1 = 1;
b_2 = 2;
r_2 = 3;
r_1 = 4;
b = b_1 - (b_1 - b_2) / (r_2 - r_1) * (r - r_1);
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Luca Ferro il 25 Gen 2023
b= @(r) b1 - ((b1-b2)*(r-r1))/(r2 -r1);
where b1,b2,r1,r2 are constants
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Call Python from MATLAB in Help Center e File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by