Octave - any examples of solving non-linear equations system with sqp function? - octave

I need example of solving non-linear euations system with octave "sqp" function, but not found anything like it (yes, maybe my knowledge of math is too bad for documentation :-) )
I have system of next 2 equations :
tan(x*y+0.3)-x^2 = 0
x^2+2*y^2-1 = 0
How can I use "sqp" for it?

Found example on http://realsuddenlike.wordpress.com/2008/07/22/nonlinear-minimization-in-octave-sequential-quadratic-programming/
For
f(x,y)=x^2+y^2.
Work's next code
function out=obj_fcn1(x)
x=x(:);
out=x’*x;
end
[x_f,j_f]=sqp(x_0,#(x)obj_fcn1(x),[],#(x)bndfcn(x));
My system can be adapted
f(x_1, ..., x_n) = f_1(x1, ..., x_n)^2 + ... + f_n(x1, ..., x_n)^2 = 0

Related

Wrong output and with a special sign "[square]" although same code works in "Octave-online"

Octave 6.1.0 (GUI)
This is a spin-off from Octave: How to turn a vector of integers into a cell array of strings?.
>> a = 1:3;
>> cellstr(int2str(a(:)))
ans =
{
[1,1] = "[square]"
}
While the output should be:
ans =
{
[1,1] = 1
[2,1] = 2
[3,1] = 3
}
octave-online.net:
How to fix this?
Only for your information and not as an answer, a workaround without this issue would be cellstr(num2str(a(:))).
This is caused by a terrible beginner's mistake.
The first helping comment under the question has already shown the way.
If the online Octave is totally different from your GUI, it is probably your fault.
In order to find the problem, check the functions that might cause the difference:
>> which('num2str')
'num2str' is a function from the file C:\Users\USERNAME\AppData\Local\Programs\GNU Octave\Octave-6.1.0\mingw64\share\octave\6.1.0\m\general\num2str.m
>> which('cellstr')
'cellstr' is a built-in function from the file libinterp/octave-value/ov-cell.cc
which('int2str')
>> which('int2str')
'int2str' is a variable`
Since I have for some unknown reason tested only other functions, but not the 'int2str', I accidentally found out about the error when using the shadowed function instead:
>> strcat('x', num2cell(int2str(1:10)))
error: int2str(10): out of bound 3 (dimensions are 1x3)
(note: variable 'int2str' shadows function)
For whatever reason, I had accidentally shadowed the function by assigning int2str = [1:3], leading to the strange behaviour.
Arbeitsumgebung = work environment:

How do I append text after an answer in Octave?

I am new to Octave and I've written the following function to combine sound pressure levels. I want to present the answer with a reference value after it. e.g.: 83 dB re: 20µPa rather than just 83, but I don't know how to add the text after the answer.
function L_total = combineLevels(Lp)
x = Lp./10;
y = 10.^x; z = sum(y);
L_total = 10*log10(z);
endfunction
Any advice would be appreciated. Thanks!
Sounds like you want to use fprintf or sprintf. These functions allow you to construct strings that interpolate values from variables. Try:
fprintf('%d dB re: %duPa\n', L_total, Lp);

Mathematica 9 - function definition problems

Please excuse the beginner question. I couldn't find an appropriate answer in any Mathematica tutorial.
I am confused why a definition as a function or a definition in terms of a simple replacement produce different results. Consider this example (Mathematica 9 code):
In[397]:= ClearAll["Global`*"]
In[398]:= Test := 3 c^2 + d^4
In[399]:= v[f_] := D[f, c]
In[400]:= v[Test]
Out[400]= 6 c
The first definition of this simple derivative function "v" acting on a variable is fine. Defining a replacement Test = ... to replace the variable produces the expected result (It derives 3c^2+d^4 with respect to c and answers 6c).
However if I define a function instead of a simple replacement this does not work:
In[401]:= TestFunction[a_, b_] := 3 a^2 + b^4
In[403]:= vFunction[f_[a_, b_]] := D[f[a, b], a]
In[405]:= vFunction[TestFunction[a, b]]
Out[405]= \!\(
\*SubscriptBox[\(\[PartialD]\), \(3\
\*SuperscriptBox[\(a\), \(2\)]\)]\((3\
\*SuperscriptBox[\(a\), \(2\)] +
\*SuperscriptBox[\(b\), \(4\)])\)\)
Why is that? I am risking to look like a moron here, but please enlighten me!
For your convenience, I uploaded a copy of my workbook here
Thanks a lot,
Michael
Do this instead
vFunction[f_,a_,b_]:=D[f[a,b],a];
and when you need derivatives simply use vFunction[TestFunction,a,b] to get it.
When you write down f[x], it means the evaluated value of f with argument value x. So, f[x] is technically not a function anymore. What you want as the argument of vFunction[] is the function TestFunction, not the evaluated value.

Dependence of variables in Mathematica 7.0

How can i get from equation one variable as a function of other variables? For example we have A*x^2 + R1*V*x + R2*v + z = 0; and i must get result V(R1,R2) = -(A*x^2 + z)/ (R1*x+R2). This is ofcourse simple example.
Investigate Solve and Reduce. This is, of course, a simple answer but should get you started.

Lua - How do I use a function from another script?

I've been looking around and I have not been able to find anything that has worked for me. I'm starting to learn more Lua and to start off I'm making a simple calculator. I was able to get each individual operation onto separate programs, but when I try to combine them I just can't get it to work. My script as it is now is
require "io"
require "operations.lua"
do
print ("Please enter the first number in your problem.")
x = io.read()
print ("Please enter the second number in your problem.")
y = io.read()
print ("Please choose the operation you wish to perform.")
print ("Use 1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division.")
op = io.read()
op = 1 then
function addition
op = 2 then
function subtraction
op = 3 then
function multiplication
op = 4 then
function division
print (answer)
io.read()
end
and my operations.lua script is
function addition
return answer = x+y
end
function subtraction
return answer = x-y
end
function multiplication
return answer = x*y
end
function division
return answer = x/y
end
I've tried using
if op = 1 then
answer = x+y
print(answer)
if op = 2 then
answer = x-y
print(answer)
and I did that completing each operation. But it doesn't work. I can't even get the error code that it's returning because it closes so fast. What should I do?
In your example, make these changes: You require operations.lua without the extension. Include parameters in your operations function definitions. Return the operation expression directly versus returning a statement like answer = x+y.
All together:
Code for operations.lua
function addition(x,y)
return x + y
end
--more functions go here...
function division(x,y)
return x / y
end
Code for your hosting Lua script:
require "operations"
result = addition(5,7)
print(result)
result = division(9,3)
print(result)
Once you get that working, try re-adding your io logic.
Keep in mind that as it's coded, your functions will be defined globally. To avoid polluting the global table, consider defining operations.lua as a module. Take a look at the lua-users.org Modules Tutorial.
The right if-then-else syntax:
if op==1 then
answer = a+b
elseif op==2 then
answer = a*b
end
print(answer)
After: please check the correct function-declaration syntax.
After: return answer=x+y is incorrect. If you want set answer's value, set without return. If you want return the sum, please use return x+y.
And I think you should check Programming in Lua.
First of all, learn to use the command line so you can see the errors (on Windows that would be cmd.exe).
Second, change the second line to require("operations"). The way you did it the interpreter expects a directory operations with an underlying script lua.lua.