Octave doesn't allow me to read correctly - octave

I am trying to read 5x^2 but octave says that it's wrong also it allows us to put (sin(x/2))^3
clc; close all; clear;
pkg load symbolic;
syms x;
f=input("Introduce la funcion ");
diff(f,x)
also there is the output with both cases
case (sin(x/2))^3
Introduce la funcion (sin(x/2))^3
ans = (sym)
2/x\ /x\
3*sin |-|*cos|-|
\2/ \2/
----------------
2
case 5x^2
Introduce la funcion 5x^2
parse error:
syntax error
>>> 5x^2
^
error: called from
symbolic at line 6 column 2

Related

Octave - System of differential equations with lsode

here is my problem. I'm trying to solve a system of two differential equations thanks to the two functions below. The part of the code that give me some trouble is the variable "rho". "rho" is a function which values are given from a file and that I tried to put in the the variable rho.
function [xdot]=f2(x,t)
# Parameters of the equations
t=[1:1:35926];
x = dlmread('data_txt.txt');
rho=x(:,4);
beta = 0.68*10^-2;
g = 1.5;
l = 1.6;
# Definition of the system of 2 ODE's :
xdot(1) = ((rho-beta)/g)*x(1) + l*x(2);
xdot(2) = (beta/g)*x(1)-l*x(2);
endfunction
.
# Initial conditions for the two variables :
x0 = [0;1];
# Definition of the time-vector -> (initial time,temporal mesh,final time) :
t = linspace (1, 10, 10000);
# Resolution with the lsode routine :
x = lsode ("f2", x0, t);
# Plot of the two curves :
plot (t,x);
When I run my code, I get the following error:
>> resolution_effective2
error: f2: A(I) = X: X must have the same size as I
error: called from
f2 at line 34 column 9
resolution_effective2 at line 8 column 3
error: lsode: evaluation of user-supplied function failed
error: called from
resolution_effective2 at line 8 column 3
error: lsode: inconsistent sizes for state and derivative vectors
error: called from
resolution_effective2 at line 8 column 3
I know that my error comes from a mismatch of size between some variable but I have been looking for the error for days now and I don't see. Could someone try to give and explain me an effective correction ?
Thank you
The error might come from your function f2. Its first argument x should have the same dimension as x0 since x0 is the initial condition of x.
In your case, whatever you intent to be the first argument of f2 is ignored since in your function you do x = dlmread('data_txt.txt'); this seems to be a mistake.
Then, xdot(1) = ((rho-beta)/g)*x(1) + l*x(2); will be a problem since rho is a vector.
You need to check the dimensions of x and rho.

octave ode program produces an unexplained error

Have a file called f.m which has the following:
function xdot = f (x,t)
xdot = -exp(-t)*x^2;
endfunction
x=lsode("f",2,(t=linspace(0,5,50)'));
plot(t,x)
Running in octave 4.2.2 or 4.2.1 produces the following error:
error: 't' undefined near line 2 column 14
error: called from
f at line 2 column 6

Octave calling function from another m file

m file that calls function nraizes(a) from another m file.
clear functions;
clc;
x = input('Insert value for a? ') ;
% call to nraizes()
w = nraizes(x)
clear functions;
nraizes.m file with nraizes() function:
printf("\n\n");
printf("nraizes por André Castro - UAB 901396");
printf("\n");
printf("Usar na próxima prompt: nraizes(valor numérico)");
printf("\n");
function n = nraizes(a)
% limpar a memoria de todas as vars e funções
clear functions;
clc;
% intervalo para x
x = 0:.1:25;
% ambas as funções h(x) e g(x)
h = #(x) cos(x);
g = #(x) exp(a*x)-1;
% traçar linha na origem das abcissas
or = x;
or(:) = 0;
% gráfico
plot(x, [h(x);g(x);or]);
axis([0, 25, -1, 1])
title("h(x),g(x)");
grid on;
printf("Fim do Script");
printf("\n");
% limpar a memoria de todas as vars e funções
clear functions;
endfunction
It always throws the following error:
warning: function 'nraizes' defined within script file '/Users/andrecastro/0_Thawed/1_UAB/1 UCS_INFORMATICA/Computacao Numerica/0 2015-2016_CN/1 Algoritmos_Octave/nraizes.m'
error: invalid use of script /Users/andrecastro/0_Thawed/1_UAB/1 UCS_INFORMATICA/Computacao Numerica/0 2015-2016_CN/1 Algoritmos_Octave/nraizes.m in index expression
error: called from:
error: /Users/andrecastro/0_Thawed/1_UAB/1 UCS_INFORMATICA/Computacao Numerica/0 2015-2016_CN/1 Algoritmos_Octave/script.m at line 19, column 7
I don't undestand why. Both .m files are in same path.
You have nraizes defined in a file with the same name as the function. You should not do it because now you have both a script and a function, both named nraizes which makes things confusing.
This is what the first warning means:
warning: function 'nraizes' defined within script file '[...]/nraizes.m'
It is warning that the function is on a script with the same name.
You can do it (it's only a warning) but you shouldn't. If you want to do it, you will need to first source the nraizes script and only after that will you have the function available:
nraizes; # source nraizes script
w = nraizes (x); # call nraizes function
That is why you got an error
error: invalid use of script [...]nraizes.m in index expression
because you tried to call a function (or index a variable -- remember that the function is not defined yet so Octave does not know) that does not exist.
However, the printf statements at the top of nraizes.m suggest that you actually want to have it as a function file only. In that case, you should drop those printf's (replace them with comments and they will be displayed when you run help nraizes) so that the first statement is the actual function definition.

Fortran does not understand call statement

I am attempting to use PGFortran for CUDA. I installed PGFortran on my computer and linked everything up to the best of my knowledge. To get going I decided to follow a tutorial listed here. When trying to compile the code:
module mathOps
contains
attributes(global) subroutine saxpy(x, y, a)
implicit none
real :: x(:), y(:)
real, value :: a
integer :: i, n
n = size(x)
i = blockDim%x * (blockIdx%x - 1) + threadIdx%x
if (i <= n) y(i) = y(i) + a*x(i)
end subroutine saxpy
end module mathOps
program testSaxpy
use mathOps
use cudafor
implicit none
integer, parameter :: N = 40000
real :: x(N), y(N), a
real, device :: x_d(N), y_d(N)
type(dim3) :: grid, tBlock
tBlock = dim3(256,1,1)
grid = dim3(ceiling(real(N)/tBlock%x),1,1)
x = 1.0; y = 2.0; a = 2.0
x_d = x
y_d = y
call saxpy<<<grid, tblock="">>>(x_d, y_d, a)
y = y_d
write(*,*) 'Max error: ', maxval(abs(y-4.0))
end program testSaxpy
I got:
PGF90-S-0034-Syntax error at or near identifier saxpy (main.cuf: 29)
0 inform, 0 warnings, 1 severes, 0 fatal for testsaxpy
The error points to the line call saxpy<<<grid, tblock="">>>(x_d, y_d, a). For some reason it apparently hates the fact that I am using <<< and >>>? Going by the tutorial those triple chevrons are meant to be there:
The information between the triple chevrons is the execution
configuration, which dictates how many device threads execute the
kernel in parallel.
Removing these chevrons would not make any sense since they are the purpose of the program. So why does PGFortran dislike this?
As for the compilation. I have followed the tutorial by using
pgf90 -o saxpy main.cuf. But since that gave an error I also tried pgf90 -Mcuda -o saxpy main.cuf. Same results.
There does seem to be a text error in that blog at the kernel invocation line:
call saxpy<<<grid, tblock="">>>(x_d, y_d, a)
tblock="" is not correct. You'll notice elsewhere in that blog text, the kernel invocation line is given correctly as:
call saxpy<<<grid,tBlock>>>(x_d, y_d, a)
So if you change that line accordingly in your actual code, I think you'll have better results.

Printing stack traces

I have a very short test file:
let print_backtrace () = try raise Not_found with
Not_found -> Printexc.print_backtrace stdout;;
let f () = print_backtrace (); Printf.printf "this is to make f non-tail-recursive\n";;
f ();
I compile and run:
% ocamlc -g test.ml
% OCAMLRUNPARAM=b ./a.out
Raised at file "test.ml", line 1, characters 35-44
this is to make f non-tail-recursive
Why isn't f listed in the stack trace? How can I write a function that will print a stack trace of the location it's called from?
The documentation for Printexc.print_backtrace says:
The backtrace lists the program locations where the most-recently raised exception was raised and where it was propagated through function calls.
It actually seems to be doing the right thing. The exception hasn't been propagated back through f.
If I move the call to Printexc.print_backtrace outside the call to f, I see a full backtrace.
$ cat test2.ml
let print_backtrace () = raise Not_found
let f () = let res = print_backtrace () in res ;;
try f () with Not_found -> Printexc.print_backtrace stdout
$ /usr/local/ocaml312/bin/ocamlc -g test2.ml
$ OCAMLRUNPARAM=b a.out
Raised at file "test2.ml", line 1, characters 31-40
Called from file "test2.ml", line 3, characters 21-39
Called from file "test2.ml", line 5, characters 4-8
Here is the code to do what I suggested. I recommend using ocamldebug if at all possible, this code is much too tricky. But it works on my system for this simple example.
let print_backtrace () =
match Unix.fork () with
| 0 -> raise Not_found
| pid -> let _ = Unix.waitpid [] pid in ()
let f () =
begin
print_backtrace ();
Printf.printf "after the backtrace\n";
end
;;
f ()
Here is a test run.
$ /usr/local/ocaml312/bin/ocamlc unix.cma -g test3.ml
$ OCAMLRUNPARAM=b a.out
Fatal error: exception Not_found
Raised at file "test3.ml", line 3, characters 17-26
Called from file "test3.ml", line 8, characters 4-22
Called from file "test3.ml", line 14, characters 0-4
after the backtrace
I realized that because of the uncaught exception, you don't really have any control over the way the child process exits. That's one reason this code is much too tricky. Please don't blame me if it doesn't work for you, but I hope it does prove useful.
I tested the code on Mac OS X 10.6.8 using OCaml 3.12.0.
Best regards,