The value of the variable might be unusued
    1 view (last 30 days)
  
       Show older comments
    
Hi, I'm writing a Lagrange polynomial code and i'm getting warning that the variables might not be unused. I'm not sure as to why since i"m defining the variable inside the function. I don't know if it's a local variable problem, but I can't figure it out. It's giving me the undefined problem bellow xi=linspace(-1,1,n);
if true
  function f=larange2(func,n)
xi=linspace(-1,1,n);
yi=zeroes(n);
yi=func(xi);
value=1;
zi=linspace(-1,1,100);
l=0;
valuel=0;
for i=0:n
    for c=0:n
        if c~=i
            value=value*((zi[i]-xi[c])/(xi[i]-xi[c]));
        end
            if c==n 
                ziy[i]=(value*yi[i]);
            end
    end
end
 for i=0:ziy.length()
    valuel=value+ziy[i]
end
end
  end
0 Comments
Accepted Answer
  Steven Lord
    
      
 on 11 Oct 2016
        Indices in MATLAB start at 1, nor 0, so you'll need to update your for loop indices. MATLAB also does not use square brackets for indexing, it uses parentheses. Once you correct the problems on lines 14 and 18 (and I'm guessing 25) that should resolve most of the red Code Analyzer messages.
The other main issue I see is this command.
ziy.length()
If ziy was an object, that would be legal syntax. Since ziy is likely a double array (unless your func function returns an object) it is not. Call the length function like this and it should work for both objects and non-objects (unless the object has overloaded length to mean something different than what MATLAB believes it should mean.)
length(ziy)
Then finally you need to make sure you're calling the function correctly. The first input should be a function handle and the second a scalar (1-by-1 number, like 5 or 23.)
I suspect you're more familiar with other programming languages like C or C++ and you're somewhat new to MATLAB (or you've used it before but you are a bit rusty), right? If so I strongly recommend that you take advantage of some of the teaching tools or resources listed in this Answers discussion to learn the basics or refresh your memory.
0 Comments
More Answers (2)
  KSSV
      
      
 on 11 Oct 2016
        It is because you have defined them but they are not taking the values what you have defined. They are taking different values accordingly.
0 Comments
See Also
Categories
				Find more on Performance and Memory in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

