Difference between revisions of "Common Matlab/Octave errors"

From Chemical Engineering @ UP wiki
Jump to: navigation, search
(Using fsolve)
(Using fsolve: (incomplete sentence))
 
(One intermediate revision by one other user not shown)
Line 4: Line 4:
 
  function thisout = functionname (thisin)
 
  function thisout = functionname (thisin)
 
you have to give the output (<tt>thisout</tt>) a value. Results of calculations stay
 
you have to give the output (<tt>thisout</tt>) a value. Results of calculations stay
within functions unless their explicitly set to an output.
+
within functions unless they are explicitly set to an output.
  
 
=== Undefined variables ===
 
=== Undefined variables ===
Line 58: Line 58:
  
 
# Mistake in equations.  If you can, work out the root manually and substitute the root into your function.  If it does not evaluate to zero, the equation may be wrong.
 
# Mistake in equations.  If you can, work out the root manually and substitute the root into your function.  If it does not evaluate to zero, the equation may be wrong.
# Wrong initial guess.  The results that you obtain are highly dependant on your initial guess, so if
+
# Wrong initial guess.  The results that you obtain are highly dependant on your initial guess, so if the solver fails to converge consider changing the starting value.

Latest revision as of 11:27, 28 May 2010

Common Matlab/Octave errors

Function outputs

Remember that when you define a function with an output:

function thisout = functionname (thisin)

you have to give the output (thisout) a value. Results of calculations stay within functions unless they are explicitly set to an output.

Undefined variables

By default, variables are private to the functions in which they are used. Therefore, if you want to use a variable within a function, pass it as an input. If you want to use a variable outside of a function, set it as an output.

Size of derivative vectors

The following error,

error: lsode: evaluation of user-supplied function failed
error: lsode: inconsistent sizes for state and derivative vectors

can be caused by two problems - the most likely being a mismatch between your system differentials' output size and the starting value vector size, when calling lsode. Make sure that there is a starting value for each of the states for which your system function calculates a derivative.

The second case is when you have a derivative output which (for some obscure reason) grows in size (dimensions).

Strange size errors

A very common error that can be hard to track down is shown below:

octave> a = [1 -(1+2); 3]
error: number of columns must match (1 != 2)

The problem is that Octave is interpreting the - as a negation (making it negative) rather than a subtraction, so there are actually two elements in the first row. The best way to avoid this problem is to use spaces on both sides of + and - signs at all times.

Plotting problems

Most common plotting problem: you plot but "nothing happens". The plot window appears but is blank. You are very probably plotting scalar values instead of vectors. Check this by repeating the plot using a more visible marker style like

plot(x, y, 's') % squares

If you see a single dot, you have forgotten to add elements to your vectors.

A common plotting pattern is

x = []
for i = 1:10
    newx = f(); % newx is calculated somehow
    x = [x newx]; % add element to vector
end
plot(x)

The single dot plot means you have probably forgotten to add elements to the vector, so the code looks like

x = newx

Using fsolve

fsolve is a numeric root finding routine. If fsolve is not working for you, you probably have one of the following problems:

  1. Mistake in equations. If you can, work out the root manually and substitute the root into your function. If it does not evaluate to zero, the equation may be wrong.
  2. Wrong initial guess. The results that you obtain are highly dependant on your initial guess, so if the solver fails to converge consider changing the starting value.