Effacer les filtres
Effacer les filtres

How to evaluate a function over a range of inputs

22 vues (au cours des 30 derniers jours)
flemingtb
flemingtb le 3 Août 2018
Modifié(e) : Fangjun Jiang le 3 Août 2018
This is probably really easy but i'm new to Matlab and can't figure out the syntax.
Example: F = ((1-n/(1+n))^2 n = [0:15]
Want to evaluate the function (F) for a given (n) is all... How do you set this up in ML?

Réponses (2)

Steven Lord
Steven Lord le 3 Août 2018
You will need to use the element-wise or array versions of the division and power operators. See this documentation page for an explanation of the different between the element-wise / array operators and the matrix operators.
As a simple example of the difference, using the times array operator ".*" and the mtimes matrix operator "*":
A = [1 2 3; 4 5 6; 7 8 9]
B = [1 4 7; 2 5 8; 3 6 9]
elementwise = A.*B
matrix = A*B

Fangjun Jiang
Fangjun Jiang le 3 Août 2018
Modifié(e) : Fangjun Jiang le 3 Août 2018
The OP is probably asking for this
>> F=@(x) ((1-x/(1+x)))^2
F =
function_handle with value:
@(x)((1-x/(1+x)))^2
>> F(15)
ans =
0.0039
More common way is to write the following and save it as F.m then you can call and run F(1)
function out=F(x)
out=((1-x/(1+x)))^2;

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by