How to evaluate a function over a range of inputs

62 visualizaciones (últimos 30 días)
flemingtb
flemingtb el 3 de Ag. de 2018
Editada: Fangjun Jiang el 3 de Ag. de 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?

Respuestas (2)

Steven Lord
Steven Lord el 3 de Ag. de 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 el 3 de Ag. de 2018
Editada: Fangjun Jiang el 3 de Ag. de 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;

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by