Scilab syntax error, unexpected end of line, expecting "," or ) its on line 14 and i dont why theres an error its correct thought - scilab

SO basically it keeps saying error at line 14 which is the "else" code is at i dont get it why it is synta error please help
clear
clc
function f=f(x)
f = x^3 + 2*x^2 - 3*x -1
endfunction
disp ("sample input"): regulaFalsi (1,2,10^-4, 100)
function regulaFalsi(a, b, TOL, N)
i = 1
FA = f(a)
finalOutput =(i, a , b , a + (b-a)/2, f(a + (b-a) /2)
printf ("%-20s%-20s%-20s%-20s%-20s\n","n","a_n","b_n","p_n","f(p_n)")
while (i <= N),
p = (a*f(b)-b*f(a))/f(b) - f(a))
FP = f(p)
if (FP == 0 | aba (f(p)) < TOL) then
break
else
printf("%-20.8g %-20.8g %-20.8g %-20.8g %-20.8g\n", i, a, b, p, f(p))
end
i = i + 1
if (FA + FP > 0) then
a = p
else
b = p
end
end
I have been trying to fix this code for my assignment but i dont know why it keeps giving me syntax error

No indentation does not matter but you wrote
disp ("sample input"): regulaFalsi (1,2,10^-4, 100)
instead of
disp ("sample input"); regulaFalsi (1,2,10^-4, 100)
a colon : instead of a semi colon ;
Moreover an "end" is missing to close the regulaFalsi function definition

In addition to Serge's answer:
regulaFalsi() is defined ''after'' the first call to it. Sure that this first call will fail.
Although finalOutput is unused (and so likely useless), the definition finalOutput =(i, a , b , a + (b-a)/2, f(a + (b-a) /2) misses a closing ). It is likely the origin of the error.
f(): here it works, but in a more general way it is not a good idea to name the function's output with the same name as the function itself.
putting clear at the head of your script(s) is not really a good idea. It is most often useless, and most often violent enough to erase useful objects, like libraries loaded on the fly, etc.
When you report an error, please report the full actual error message. It is most often more useful than only comments or "personal translation" of the error.

Related

SingularException(2) when invoking multivariate Newton's method in Julia

I have implemented the multivariate Newton's method in Julia.
function newton(f::Function, J::Function, x)
h = Inf64
tolerance = 10^(-10)
while (norm(h) > tolerance)
h = J(x)\f(x)
x = x - h
end
return x
end
On the one hand, when I try to solve the below system of equations, LoadError: SingularException(2) is thrown.
f(θ) = [cos(θ[1]) + cos(θ[2] - 1.3),
sin(θ[1]) + sin(θ[2]) - 1.3]
J(θ) = [-sin(θ[1]) -sin(θ[2]);
cos(θ[1]) cos(θ[2])]
θ = [pi/3, pi/7]
newton(f, J, θ)
On the other hand, when I try to solve this other system
f(x) = [(93-x[1])^2 + (63-x[2])^2 - 55.1^2,
(6-x[1])^2 + (16-x[2])^2 - 46.2^2]
J(x) = [-2*(93-x[1]) -2*(63-x[2]); -2*(6-x[1]) -2*(16-x[2])]
x = [35, 50]
newton(f, J, x)
no errors are thrown and the correct solution is returned.
Furthermore, if I first solve the second system and then try to solve the first system, which normally throwns SingularException(2), I now instead encounter no errors but am returned a totally inaccurate solution to the first system.
What exactly is going wrong when I try to solve the first system and how can I resolve the error?

Making a Star Triangle in SML

I have just started coding in SMLNJ and have been having some trouble making a program that returns a string in a triangular star pattern. For example triangle(5) should output:
*****
****
***
**
*
My code so far is:
fun triangle(x) =
if (x = 0) then "\n"
else
let
fun makeTriangle(n) =
if(n = 0) then "\n" else "*"^makeTriangle(n-1);
in
makeTriangle(x);
end
triangle(x-1)
I am getting the error "triangle.sml:9.3 Error: syntax error: inserting EQUALOP". Any help would be appreciated.
There are at least two issues with your code:
First, there is a simple operator precedence issue:
if(n = 0) then "\n" else "*"^makeTriangle(n-1)
parses as
(if(n = 0) then "\n" else "*") ^ makeTriangle(n-1)
rather than your intended
if(n = 0) then "\n" else ("*" ^ makeTriangle(n-1))
The solution is to put in the needed parentheses.
Another issue is the stray line triangle(x-1) at the bottom of the function. It is unrelated to the code above it. If your intention is to concatenate it to the result of the function call makeTriangle(x) then you would need to do the explicit concatenation. There really shouldn't be anything in your function definition after the end since that end terminates the else part.
A minor issue: since your function makeTriangle inserts "\n", your code (after fixed) will have two "\n" at the bottom of the triangle. If that isn't what you want, perhaps you can think about the basis case (n=0).
Since John already explained some of the issues with your code, and since this seems like an exercise, here are two ways you could solve it differently:
Recursively, using pattern matching:
fun repeat (0, _) = []
| repeat (n, x) = x :: repeat (n-1, x)
fun triangle 0 = ""
| triangle n = implode (repeat (n, #"*")) ^ "\n" ^ triangle (n-1)
There's a library function called List.tabulate of which repeat is a special case:
fun repeat (n, x) = List.tabulate (n, fn _ => x)
But actually, triangle itself fits pretty well within List.tabulate:
fun triangle n =
concat (List.tabulate (n, fn i => implode (repeat (15 - i, #"*")) ^ "\n"))

How to detect numbers from one column in another and write data from another column in R?

I'm very new at R language. I tried to make simple code to find some data in different intervals with information about start-end of interval and table with bigger intervals for now, I got several codes for solving but none of them work.
The main idea is that I have several variables which represent different arrays (i,k,j). Code, in my opinion, should look for each array in another table for two things (if it bigger than the first column and if it smaller then second, if both true - right all this to another table and go to other intervals).
if(mydatatable[k,21]>=mydatatable[i,16]){
if(mydatatable[k,21]<=mydatatable[j,18])
shifr[n,1]<-n&shifr[n,2]<-mydata[k,21]&shifr[n,3]<-mydata[k,22]&i+1&j+1&k+1&n+1
else i+1&j+1&k+1
}
else {
if(mydatatable[i,16]==0) end
else i+1&j+1&k+1
}
for this code several errors
Error in if (mydatatable[k, 21] >= mydatatable[i, 16]) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In Ops.factor(mydatatable[k, 21], mydatatable[i, 16]) :
‘>=’ not meaningful for factors
I wonder, why programm thinks, that mydatatable is factor? Why it should be some TRUE/FALSE value, I thought, that it was already established in formula.
The second code is pretty much the same and it even might work.
I preestablished the values i,k and j as 1 (i=1, k=1, j=1)
But there comes a error
> ifelse(mydatatable[k,21]>=mydatatable[i,16],
+ ifelse(mydatatable[k,21]<=mydatatable[j,18],
+ shifr[n,1]<-n&shifr[n,2]<-mydata[k,21]&shifr[n,3]<-mydata[k,22]&i+1&j+1&k+1&n+1,i+1&j+1&k+1),
+ ifelse(mydatatable[i,16]==0,
+ end),
+ i+1&j+1&k+1)
Error in ifelse(mydatatable[k, 21] >= mydatatable[i, 16], ifelse(mydatatable[k, :
unused argument (i + 1 & j + 1 & k + 1)
I'm confused, why it's happening.
Please, help.
Here is an example of data. What I want to get is demonstrated here https://1drv.ms/f/s!Aj4W4aYSeYkGiIFKHG0TV-TRQvWaIQ
Here what I got, after several use of data.table::foverlaps (I fixed some problems, but initially I got this)
> data.table::foverlaps(int1,sh2,by.x=c("start","end"),by.y=c("start","end"))
Error in data.table::foverlaps(int1, sh2, by.x = c("start", "end"), by.y = c("start", :
The first 2 columns of y's key is not identical to the columns specified in by.y.
I have also got some progress with previous code. Several problems: first, how to unite several commands (I used & and ; but none of them worked properly in shifr[n,1]<-n;shifr[n,2]<-sh[k,1];shifr[n,3]<-sh[k,1];i+1&j+?)
Second, is about Error in FUN(left, right).
> ifelse(sh[k,1]>=int[i,1],
+ + ifelse(sh[k,2]<=int[j,2],
+ + shifr[n,1]<-n;shifr[n,2]<-sh[k,1];shifr[n,3]<-sh[k,1];i+1;j+1;k+1;n+1,i+1;j+1;k+1),
Error: unexpected ';' in:
" + ifelse(sh[k,2]<=int[j,2],
+ shifr[n,1]<-n;"
> + i+1;j+1;k+1
[1] 16
[1] 16
[1] 65
> ifelse(sh[k,1]>=int[i,1],
+ + ifelse(sh[k,2]<=int[j,2],
+ + shifr[n,1]<-n;shifr[n,2]<-sh[k,1];shifr[n,3]<-sh[k,1];i+1&j+1&k+1&n+1,i+1&j+1&k+1),
Error: unexpected ';' in:
" + ifelse(sh[k,2]<=int[j,2],
+ shifr[n,1]<-n;"
> + i+1&j+1&k+1
[1] TRUE
> ifelse(sh[k,1]>=int[i,1],
+ + ifelse(sh[k,2]<=int[j,2],
+ + shifr[n,1]<-n&shifr[n,2]<-sh[k,1]&shifr[n,3]<-sh[k,1]&i+1&j+1&k+1&n+1,i+1&j+1&k+1),
+ + i+1&j+1&k+1
+ )
Error in FUN(left, right) :
operations are possible only for numeric, logical or complex types
This code is, actually, awful. I eventually done it like that(in python):
for i in interval:
...
if float(i) < intervals[b][0]:
print("Interval are too high", intervals[b][1])
break
....
if float(i) >= intervals[b - 1][1]:
....

Using solve and/or linsolve with the symbolic toolbox in R2010b

I asked a question a few days ago here and got an answer that seems like it would work- it involves using linsolve to find the solutions to a system of equations that are all modulo p, where p is a non-prime integer.
However, when I try to run the commands from the provided answer, or the linsolve help page, I get an error saying linsolve doesn't support arguments of type 'sym'. Is using linsolve with sym variables only possible in R2013b? I've also tried it with my school's copy, which is R2012b. Here is the code I'm attempting to execute (from the answer at the above link):
A = [0 5 4 1;1 7 0 2;8 1 0 2;10 5 1 0];
b = [2946321;5851213;2563617;10670279];
s = mod(linsolve(sym(A),sym(b)),8)
And the output is:
??? Undefined function or method linsolve' for input arguments of type 'sym'.
I've also tried to use the function solve for this, however even if I construct the equations represented by the matrices A and b above, I'm having issues. Here's what I'm attempting:
syms x y z q;
solve(5*y + 4*z + q == 2946321, x + 7*y + 2*q == 5851213, 8*x + y + 2*q == 2563617, 10*x + 5*y + z == 10670279,x,y,z,q)
And the output is:
??? Error using ==> char
Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 169
vc = char(v);
Error in ==> solve at 67
[eqns,vars] = getEqns(varargin{:});
Am I using solve wrong? Should I just try to execute my code in R2013b to use linsolve with symbolic data types?
The Symbolic Math toolbox math toolbox has changed a lot (for the better) over the years. You might not have sym/linsolve, but does this work?:
s = mod(sym(A)\sym(b),8)
That will basically do the same thing. sym/linsolve just does some extra input checking and and rank calculation to mirror the capabilities of linsolve.
You're using solve correctly for current versions, but it looks like R2010b may not understand the == operator (sym/eq) in this context. You can use the old string format to specify your equations:
eqs = {'5*y + 4*z + q = 2946321',...
'x + 7*y + 2*q = 5851213',...
'8*x + y + 2*q = 2563617',...
'10*x + 5*y + z = 10670279'};
vars = {'x','y','z','q'};
[x,y,z,q] = solve(eqs{:},vars{:})

trapezodial rule matlab

I want to integrate "\int_{0}^{1}(exp(-int_{0}^{y}f(x)dx))dy" with my basic trapezoid algorithm. I recieve an error declaration, but I should define g as a function. Do you have any idea how to do it?
Thanks a lot for any answer!
function y = trapapadbl(low1, up1,low2,up2,intstep1,intstep2,f)
g = 0;
step1 = (up1 - low1) / intstep1;
step2 = (up2 - low2) / intstep2;
for j = low1 : step1 : up1
g = g + feval(f,j);
end
g = #(y)(g - (feval(f, low1) + feval(f, up1))/2) * step1;
for i = low2 : step2 : up2
y= y + feval(g,i);
end
y= (y - (feval(g, low2) + feval(g, up2))/2) * step2;
>> trapapadbl(0,1,0.1,0,1,0.1,#sin)
??? Undefined function or variable "y".
Error in ==> trapapadbl at 12
y= y + feval(g,i);
Without working too hard to try to understand your code (!) the error is that y was never initialized. You can't add anything to y until it has a value. When I initialize y to 0, the code runs, but I get 0 as an output, which is not what happens when you integrate sin from 0 to 1. I may be calling the function wrong, but it's something to look out for!
Furthermore, your code is confusing, because you use the variable g as both a double (a number) and a function, even in the same line! The same problem happens as y is the input to your anonymous function, but also a double later on. It's syntactically correct, but a little hard to read. Consider using a different variable name, or including clear comments (or both!)

Resources