I wrote a model to solve using dynare in octave. After writing the model, I save it with “.m” format. Then I type “dynare filename” in octave’s command window to run the model but instead the file gets deleted.
Oddly, it doesn’t happen if I use the command on a file with “.mod” format.
I’m using Octave 5.2.0 since the dynare only works with this version.
Please help me if you can. Tnx a lot.
I guess you used Dynare 4.5 instead of the recent stable release 4.6. As documented in the manual, the only supported file-types in Dynare are .mod and .dyn. You cannot name your file .m as Dynare will preprocess your .mod-file and create an m-file with the same name. Usually, you would get an explicit error message. However, in Octave, the following code is executed first:
% Workaround for a strange bug with Octave: if there is any call to exist(fname)
% before the call to the preprocessor, then Octave will use the old copy of
% the .m instead of the newly generated one. Deleting the .m beforehand
% fixes the problem.
if isoctave && length(dir([fname(1:(end-4)) '.m'])) > 0
delete([fname(1:(end-4)) '.m'])
end
Dynare 4.6 instead moved to Matlab-classes. There you will get an explicit error message.
I'm trying to analyze the system
20*e(0.9*s)
-------------
(s+10)
using GNU octave and the control package, but I cannot find the way to introduce the exponential (the exp octave function does not work).
The code I tried to write is
pkg load control
s = tf('s')
g = 20*exp(0.9*s)/(s+10)
but the following error is shown: error: exp: not defined for class
I tried searching the web a bit, without success.
Thanks in advance
For my summer programming course I need to compile and test several functions from an ocaml file (a .ml file) our teacher gave us. I've never programmed in ocaml before, but I downloaded it on my ubuntu vm and not I can't figure out how to compile it and test the functions from the terminal.
I'm also curious if I need to add print statements to the code in order to test them, since ocaml doesn't require main methods. If so, how do I print the return value of a function?
Thank you for your help, I apologize if this is a newbie question.
Make sure that you have installed OCaml properly, by running the OCaml interactive toplevel (don't type $ this is a prompt from your shell):
$ ocaml
It should show something like this:
OCaml version 4.07.0
#
The # symbol is a prompt, you can type OCaml definitions there and send them to the interpreter using the ;; terminating sequence (again don't type # it is also a prompt symbol), e.g.,
# print_endline "Hello, world";;
Hello, world
- : unit = ()
#
Hint: to enable history install rlwrap with sudo apt install rlwrap and run the ocaml toplevel as
$ rlwrap ocaml
Now, we are ready to compile our first program. In OCaml, like in Python, all top-level definitions in your program are evaluated in order of their appearance, therefore you don't need to have a special main function. Despite this fact, I'm usually defining one, using the following idiom
let main () =
print_endline "Hello, world"
(* the entry point - just call the main function *)
let () = main ()
Now, create a new folder (make sure it is empty)
$ mkdir my-first-ocaml-program
$ cd my-first-ocaml-program
and put the OCaml code above into a file named test.ml (the filename doesn't have any special meaning for the compiler toolchain, but I will reference this name in the shell commands below).
let's test that everything is correct, by printing the contents of the test.ml file
$ cat test.ml
and the output should be
let main () =
print_endline "Hello, world"
(* the entry point - just call the main function *)
let () = main ()
Now, let's compile and run at the same time,
$ ocamlbuild test.native --
And we should see the "Hello, world",
Finished, 4 targets (4 cached) in 00:00:00.
Hello, world
The first line is the output from the compiler (ignore it, unless it is different). Starting from the second line it is our output. Here are some explanations on the build one-liner, ocamlbuild test.native. It uses ocamlbuild, an easy to use but powerful OCaml build tool. The test.native tells ocamlbuild that you want to build a native (machine code) binary and that test.ml is the main source file. You can also ask to build a bytecode binary, e.g., test.byte. This is called a target in ocamlbuild parlance. The -- is optional, and it tells ocamlbuild to run the built target as soon as it is ready. Any argument past -- is passed to your program as command line arguments.
What about larger programs or programs with dependencies? The good news is that you can put your code in several files in the same folder, and ocamlbuild will be clever enough to build them in the proper order (it will do the dependency analysis, compiling, and linking for you - all seamlessly). If you need to use some external package, then you can specify it via the -pkg option. For example, let's assume that we're using the fileutils package to implement our version of the ls program1. Let's update our test.ml so that it now is having the following contents:
$ cat test.ml
let main () =
FileUtil.ls "." |> List.iter print_endline
(* the entry point - just call the main function *)
let () = main ()
and, as usual, build and print in one
$ ocamlbuild -pkg fileutils test.native --
Finished, 4 targets (4 cached) in 00:00:00.
./test.native
./_build
./test.ml
1) How to install a package is system dependent. E.g., if you're using apt to install OCaml then you can do sudo apt install libfileutils-ocaml-dev. If you're using opam (the recommended way), then it is just opam install fileutils. In any case, package installation is out of the scope of this question, I'm assuming that in your course you would be using some packages pre-installed. We use fileutils here just as an example, which you can easily adapt to your own purposes.
lets say I have a test.m octave script
test.m
x = 1;
A % Syntax error, A isn't defined
so I run the script with
octave test.m
What I'm interested in is how do I get octave to return non-zero exit status if the script contains a syntax error? Note, I am not interested in encapulating the code with if-else statements and do exit(-1). I am interested in a solution that tells octave to return a non-zero value if the script contains syntax errors.
Edit: I was using Octave 4.2 that comes default with Ubuntu 18.04. Commenters have suggested Octave 5+ does not exhibit this behaviour.
Never mind I solved it. To give some context why I needed this behaviour, its because I'm trying to write the octave scripts that basically test octave functions I write.
Now more often than not I will have written scripts that contain syntax errors, and octave will raise errors while parsing the scripts. However, the return status is always 0 if you run your script in a straight forward manner like:
octave script.m
regardless if your octave script contains syntax errors or not. I however did not want to surround every script I write with if-else or try-catches, that is why I opened this question on SO.
A workaround I have found is just to source the script by using the --eval flag. i.e.
octave --eval "source('script.m');"
This way if your script does contain syntax errors the return value / status is a non-zero exit value. In this case I believe octave defaults to 1.
I cannot reproduce the behaviour in your question. In octave 5 at least, doing
octave myscript.m
results in a return value of 1 in case of error.
Same as doing
octave --eval "source('myscript.m')"
I'm beginner in Octave. I'm sorry if I ask a silly question.
I want to use feedback function in Control package.
I installed this package, using following command.
pkg install -forge control
The Installation seems to be successful, and following directory is automatically generated.
~/octave/control-2.6.5
But Octave says following messages when I enter "feedback" in Octave.
warning: the 'feedback' function belongs to the control package from Octave Forge but has not yet been implemented.
"feedback.m" file exists in "~/octave/control-2.6.5/#lti".
How do I use this .m file.
Any help would be appreciated. Thanks in advance.
OS: Ubuntu 14.04LTS
Octave version: 3.8.1
You have to load it before you can use it. Just execute
pkg load control
on the octave prompt.
Edit:
After this you can do:
s1 = tf (1, [1 2]);
s2 = tf (1, [1 5]);
cl = feedback (s1, s2)
which outputs:
Transfer function 'cl' from input 'u1' to output ...
s + 5
y1: --------------
s^2 + 7 s + 11
Use bode and step to evaluate the result. If you want help type help #lti/feedback on the octave prompt.