How to evaluate a function over a range of inputs

22 次查看(过去 30 天)
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?

回答(2 个)

Steven Lord
Steven Lord 2018-8-3
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 2018-8-3
编辑:Fangjun Jiang 2018-8-3
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;

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by