Octave goes in Waiting... when solve() function is used - octave

I've installed and loaded the symbolic package that becomes available from optim package to obtain the syms function (like in MATLAB) but when I use solve() function the command window goes in Waiting mode like
Waiting..........
My code is given below:
syms s T K D1 D2 D3 theta1 theta2 theta3 J1 J2 J3
eq1 = (s * D1 + K + J1 * s ^ 2)* theta1 - K * theta2 == T;
eq2 = -K * theta1 + (J2 * s ^ 2 + K + D2 * s) * theta2 - D2 * s * theta3 == 0;
eq3 = -D2 * s * theta2 + (D3 * s + J3 * s ^ 2 + D2 * s) * theta3 == 0;
S = solve(eq1, eq2, eq3)
but if I manually solve it by inverse method, it gives the answer instantly. Kindly help to solve this bug.

Related

Lagrange Polynomial not plotting in Octave

The question requires a Lagrange polynomial L(c) to replace the complicated-looking function, f. Also, plot function f and L(c) between [1:0; 10:0]. However, after the code computes the values for k, it cannot seem to create a plot and a graph. What am I doing wrong? Attached is the code below. Thank You!
clear
pkg load symbolic
graphics_toolkit('gnuplot')
syms m x
c = []
for i = 1: 0.5 : 10
c = [c,i]
endfor
d = length(c)
k = []
f = 3.* (1 .- ( 8 ./ vpa(pi.^2) .* ((1 ./ cosh(vpa(pi) .* vpa(x) ./ 2)) .+ symsum(1 ./ ((2 .* m .+ 1).^2 .* cosh((2 .* m .+ 1) .* vpa(pi) .* vpa(x) ./ 2)), m,1 ,Inf)))) ./ ((1 .- ( 192 ./ vpa(x) .* vpa(pi.^5)).*(tanh(vpa(pi) .* vpa(x) ./ 2) .+ symsum(tanh((2 .* m .+ 1) .* vpa(pi) .* vpa(x) ./ 2) ./ (2 .* m .+ 1).^5, m,1 ,Inf))));
for i = 1:d
A = (1 - ( 192 / vpa(c(i)) * vpa(pi^5))*(tanh(vpa(pi) * vpa(c(i)) / 2) + symsum(tanh((2 * m + 1) * vpa(pi) * vpa(c(i)) / 2) / (2 * m + 1)^5, m, 1, Inf)))
B = 1 - ( 8 / vpa(pi^2) * ((1 / cosh(vpa(pi) * vpa(c(i)) / 2)) + symsum(1 / ((2 * m + 1)^2 * cosh((2 * m + 1) * vpa(pi) * vpa(c(i)) / 2)), m, 1, Inf)))
K = 3 * B / A
k = [k,K]
endfor
col = c(1)
matrixc = [];
for m = 1:d;
prod = 1;
for t = 1:d
if col == ((m+1)/2)
col = col .+ ((10-1)/(d-1));
endif
sol = (x.-vpa(col))./(vpa((m+1)/2).-vpa(col));
prod = prod*sol;
col = col .+ ((c(2)-c(1))/(d-1));
endfor
h = k(m);
p = vpa(h).*prod;
matrixc = [matrixc,p];
col = 1;
endfor
L=sum(matrixc);
L
ezplot(L,[c(1),c(d)]);
hold on;
k(1)
plot(c,k);
legend('Lagrange Polynomial','K(x)');
xlabel('x-axis');
ylabel('y-axis');```
You are trying to plot a symbolic variable.
I don't know if this is possible in general, but in this particular case at least the conversion is causing problems
Collect the values of the symbolic variable first, by casting to double, and then you can plot as intended:
plot( c, double(k) );

Amplitude increasing in the verlet method in backward difference

I am facing trouble in the increasing oscillation on a simple harmonic oscillator using a backward difference. here is my code in Scilab
function [x] = back(h, tf)
k = 2;
m = 1;
i = 2;
t(i - 1) = 0;
x(i - 1) = 10;
v(i - 1) = 0;
t(i) = t(i - 1) + h
v(i) = v(i - 1) - h * (k / m) * x(i - 1)
while t(i) < tf
t(i + 1) = t(i) + h
x(i + 1) = x(i - 1) - 2 * (k / m) * v(i) * h
i = i + 1
end
plot(t, x, 'b');
endfunction
From your code, I suppose that you are trying to implement the velocity-Verlet scheme. Here is its implementation for a simple oscillator with the differential equation:
function [x] = back(h, tf)
k = 2;
m = 1;
t = 0:h:tf;
x(1) = 10;
v(1) = 0;
for i=2:length(t)
x(i) = x(i - 1) + v(i - 1) * h - k / m * x(i-1) * h^2 / 2;
v(i) = v(i - 1) - k / m * (x(i) + x(i-1)) * h / 2;
end
plot(t, x, 'b');
endfunction
[x] = back(0.01, 10)
I'm not quite sure what you are trying to achieve, neither if your math is correct. But assuming that you want to solve the numerical problem of:
//coefficients of:
k = 2.;
m = 1.;
// with an initial condition of:
t(1) = 0.;
x(1) = 10.;
v(1) = 0.;
// time paramters:
N = 50;
tf = 10;
h = tf / 50.;
for ii = 2:N
t(ii) = t(ii - 1) + h;
x(ii) = x(ii - 1) - 2 * (k / m) * v(ii - 1) * h
v(ii) = v(ii - 1) - h * (k / m) * x(ii - 1)
disp(x(ii))
end
plot(t, x, 'b');
will result in:
which doesn't seem right but anyway. Please check your math again.

Octave ODE solver: issues with state & derivative vectors

I have the code to solve a problem of non-stationary heat transfer (no, this is not homework, but the code is taken from a textbook), which requires solving a set of ODEs with the Method of Lines, and goes as follows:
%Problem P6_08A
clear, clc, format short g, format compact
tspan = [0 1000.]; % Range for the independent variable
y0 = [100.; 100.; 100.; 100.; 100.; 100.; 100.; 100.; 100.]; % Initial values for the dependent variables
%- - - - - - - - - - - - - - - - - - - - - -
function dYfuncvecdt = ODEfun(Yfuncvec, t);
Yfuncvec = [];
T2 = Yfuncvec(1);
T3 = Yfuncvec(2);
T4 = Yfuncvec(3);
T5 = Yfuncvec(4);
T6 = Yfuncvec(5);
T7 = Yfuncvec(6);
T8 = Yfuncvec(7);
T9 = Yfuncvec(8);
T10 = Yfuncvec(9);
alpha = .00002;
deltax = .1;
T1 = 0;
T11 = (4 * T10 - T9) / 3;
dT2dt = alpha / (deltax ^ 2) * (T3 - (2 * T2) + T1);
dT3dt = alpha / (deltax ^ 2) * (T4 - (2 * T3) + T2);
dT4dt = alpha / (deltax ^ 2) * (T5 - (2 * T4) + T3);
dT5dt = alpha / (deltax ^ 2) * (T6 - (2 * T5) + T4);
dT6dt = alpha / (deltax ^ 2) * (T7 - (2 * T6) + T5);
dT7dt = alpha / (deltax ^ 2) * (T8 - (2 * T7) + T6);
dT8dt = alpha / (deltax ^ 2) * (T9 - (2 * T8) + T7);
dT9dt = alpha / (deltax ^ 2) * (T10 - (2 * T9) + T8);
dT10dt = alpha / (deltax ^ 2) * (T11 - (2 * T10) + T9);
dYfuncvecdt = [dT2dt; dT3dt; dT4dt; dT5dt; dT6dt; dT7dt; dT8dt; dT9dt; dT10dt];
end
%
[y, t]=lsode(#ODEfun, y0,tspan);
disp([y0 ODEfun(tspan(1),y0)]);
disp(' Variable values at the initial point ');
disp([' t = ' num2str(tspan(1))]);
disp(' y dy/dt ');
for i=1:size(y,2)
disp([' Solution for dependent variable y' int2str(i)]);
disp([' t y' int2str(i)]);
disp([t y(:,i)]);
plot(t,y(:,i));
title([' Plot of dependent variable y' int2str(i)]);
xlabel(' Independent variable (t)');
ylabel([' Dependent variable y' int2str(i)]);
pause
end
%EOF
Trying to run the code returns an error:
error: ODEfun: A(I): index out of bounds; value 1 out of bound 0
error: called from:
error: ODEfun at line 8, column 4
error: lsode: evaluation of user-supplied function failed
error: lsode: inconsistent sizes for state and derivative vectors
error: $path/lines01_diff_eq.m at line 33, column 5
It is the part index out of bounds; value 1 out of bound 0 that I can't make sense of. Can somebody help?
It turns out that that line:
disp([y0 ODEfun(tspan(1),y0)]);
Should instead become:
disp([y0 ODEfun(y0, tspan(1)]);
And also the lines:
disp([t y(:,i)]);
plot(t,y(:,i));
Should be modified to:
disp([tspan y(:,i)']);
plot(tspan, y(:,i)');
In order to make the code execute without errors.

Apache POI rate formula not working if data is big

Rate Formula is not working as expected for big values...
RATE(85.77534246575343, -1589.0, -18664.0, 5855586.0) in physical file it returns 0.05819488005
if the same formula we tried to set through POI returns 0.009056339275922086..
Even we tried to save the excel and open same 0.009056339275922086 is returned..
Code used to set in POI :
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFRow row = sheet.createRow(1);
XSSFCell cell = row.createCell(1);
cell.setCellType(CellType.NUMERIC);
cell.setCellFormula("RATE(85.77534246575343, -1589.0, -18664.0, 5855586.0)");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateInCell(cell);
cell.getNumericCellValue();
The Rate function of apache poi states that it "// find root by Newton secant method". That's nonsense since Secant method is only a Quasi-Newton method. And "If the initial values are not close enough to the root, then there is no guarantee that the secant method converges.".
So the default guess of 0.1 seems not "close enough" and so if we are using cell.setCellFormula("RATE(85.77534246575343, -1589.0, -18664.0, 5855586.0, 0, 0.06)"); - note the explicit setting of type and guess properties and having the guess property "close enough" to the result of 0.05819488005- then the formula evaluates properly.
If apache poi really would use Newton's method, then the function would evaluate properly also using the default guess of 0.1. The disadvantage of Newton's method is that it requires the evaluation of both f and its derivative f′ at every step. So it may be slower than the Secant method in some cases.
Example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
public class ExcelRATEFunction {
private static double calculateRateNewton(double nper, double pmt, double pv, double fv, double type, double guess) {
int FINANCIAL_MAX_ITERATIONS = 20;
double FINANCIAL_PRECISION = 0.0000001;
double y, y1, xN = 0, f = 0, i = 0;
double rate = guess;
//find root by Newtons method (https://en.wikipedia.org/wiki/Newton%27s_method), not secant method!
//Formula see: https://wiki.openoffice.org/wiki/Documentation/How_Tos/Calc:_Derivation_of_Financial_Formulas#PV.2C_FV.2C_PMT.2C_NPER.2C_RATE
f = Math.pow(1 + rate, nper);
y = pv * f + pmt * ((f - 1) / rate) * (1 + rate * type) + fv;
//first derivative:
//y1 = (pmt * nper * type * Math.pow(rate,2) * f - pmt * f - pmt * rate * f + pmt * nper * rate * f + pmt * rate + pmt + nper * pv * Math.pow(rate,2) * f) / (Math.pow(rate,2) * (rate+1));
y1 = (f * ((pmt * nper * type + nper * pv) * Math.pow(rate,2) + (pmt * nper - pmt) * rate - pmt) + pmt * rate + pmt) / (Math.pow(rate,3) + Math.pow(rate,2));
xN = rate - y/y1;
while ((Math.abs(rate - xN) > FINANCIAL_PRECISION) && (i < FINANCIAL_MAX_ITERATIONS)) {
rate = xN;
f = Math.pow(1 + rate, nper);
y = pv * f + pmt * ((f - 1) / rate) * (1 + rate * type) + fv;
//first derivative:
//y1 = (pmt * nper * type * Math.pow(rate,2) * f - pmt * f - pmt * rate * f + pmt * nper * rate * f + pmt * rate + pmt + nper * pv * Math.pow(rate,2) * f) / (Math.pow(rate,2) * (rate+1));
y1 = (f * ((pmt * nper * type + nper * pv) * Math.pow(rate,2) + (pmt * nper - pmt) * rate - pmt) + pmt * rate + pmt) / (Math.pow(rate,3) + Math.pow(rate,2));
xN = rate - y/y1;
++i;
System.out.println(rate+", "+xN+", "+y+", "+y1);
}
rate = xN;
return rate;
}
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellFormula("RATE(85.77534246575343, -1589.0, -18664.0, 5855586.0, 0, 0.06)");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
CellType celltype = evaluator.evaluateFormulaCellEnum(cell);
double value = 0.0;
if (celltype == CellType.NUMERIC) {
value = cell.getNumericCellValue();
System.out.println(value);
}
workbook.setForceFormulaRecalculation(true);
value = calculateRateNewton(85.77534246575343, -1589.0, -18664.0, 5855586.0, 0, 0.1);
System.out.println(value);
workbook.write(new FileOutputStream("ExcelRATEFunction.xlsx"));
workbook.close();
}
}

Canvas quadraticCurve center point

I want need to know how detect center coordinates of quadraticCurve in HTML5 canvas. I want to draw arrow in this center point of curve.
There is my draw curve method:
function draw_curve(Ax, Ay, Bx, By, M, context) {
var dx = Bx - Ax,
dy = By - Ay,
dr = Math.sqrt(dx * dx + dy * dy);
// side is either 1 or -1 depending on which side you want the curve to be on.
// Find midpoint J
var Jx = Ax + (Bx - Ax) / 2
var Jy = Ay + (By - Ay) / 2
// We need a and b to find theta, and we need to know the sign of each to make sure that the orientation is correct.
var a = Bx - Ax
var asign = (a < 0 ? -1 : 1)
var b = By - Ay
var bsign = (b < 0 ? -1 : 1)
var theta = Math.atan(b / a)
// Find the point that's perpendicular to J on side
var costheta = asign * Math.cos(theta)
var sintheta = asign * Math.sin(theta)
// Find c and d
var c = M * sintheta
var d = M * costheta
// Use c and d to find Kx and Ky
var Kx = Jx - c
var Ky = Jy + d
// context.bezierCurveTo(Kx, Ky,Bx,By, Ax, Ax);
context.quadraticCurveTo(Kx, Ky, Bx, By);
// draw the ending arrowhead
var endRadians = Math.atan((dx) / (dy));
context.stroke();
var t = 0.5; // given example value
var xx = (1 - t) * (1 - t) * Ax + 2 * (1 - t) * t * Kx + t * t * Bx;
var yy = (1 - t) * (1 - t) * Ay + 2 * (1 - t) * t * Ky + t * t * By;
var k = {};
k.x = xx;
k.y = yy;
SOLVED BY THIS CODE, T is parameter which set position on the curve:
var t = 0.5; // given example value
var xx = (1 - t) * (1 - t) * Ax + 2 * (1 - t) * t * Kx + t * t * Bx;
var yy = (1 - t) * (1 - t) * Ay + 2 * (1 - t) * t * Ky + t * t * By;
var k = {};
k.x = xx;
k.y = yy;