How can I get my fucntion to accept doubles.
    6 views (last 30 days)
  
       Show older comments
    
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.
0 Comments
Accepted Answer
  Stephen23
      
      
 on 4 Dec 2017
        
      Edited: Stephen23
      
      
 on 4 Dec 2017
  
      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 Comments
More Answers (0)
See Also
Categories
				Find more on Logical in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
