How can I get my fucntion to accept doubles.

6 ビュー (過去 30 日間)
Matthew Robinson
Matthew Robinson 2017 年 12 月 4 日
編集済み: Stephen23 2017 年 12 月 4 日
Hi there,
I'm looking to create a function, which operates from its own file, where I can give it a double value, it does some math, and then it provides an answer based on the equation and variables detailed in the function. For example, the function file that I have created contains code that is something like:
function f = CalcGaussValue(x)
a1 = 10.25;
b1 = 0.005067;
c1 = 0.009325;
a2 = 267.1;
b2 = 0.0148;
c2 = 0.03119;
f(x) = a1*exp(-((x-b1)/c1)^2) + a2*exp(-((x-b2)/c2)^2)
end
It runs perfectly fine with integers, yet come back with an error when it is given a double (which is what I would prefer to give it). When using an iput value of 1.1 for example, the error states: "Attempted to access (1.1); index must be a positive integer or logical"
Any help here would be greatly appreciated.

採用された回答

Stephen23
Stephen23 2017 年 12 月 4 日
編集済み: Stephen23 2017 年 12 月 4 日
This makes no sense:
f(x) = ...
because you are trying to do is use x as an index, which clearly makes no sense for non-integer values. Probably what you meant was to simply allocate to a variable:
f = a1*exp(-((x-b1)/c1)^2) + a2*exp(-((x-b2)/c2)^2);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by