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{:})
Related
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.
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?
While solving a differential equation on satellite motion encountered this error:
dt <= dtmin. Aborting. If you would like to force continuation with dt=dtmin, set force_dtmin=true
Here is my code:
using JPLEphemeris
spk = SPK("some-path/de430.bsp")
jd = Dates.datetime2julian(DateTime(some-date))#date of the calculatinons
yyyy/mm/dd hh/mm/ss
jd2 = Dates.datetime2julian(DateTime(some-date))#date of the calculatinons
yyyy/mm/dd hh/mm/ss
println(jd)
println(jd2)
st_bar_sun = state(spk, 0, 10, jd)
st_bar_moon_earth = state(spk, 0, 3, jd)
st_bar_me_earth = state(spk, 3, 399, jd)
st_bar_me_moon = state(spk, 3, 301, jd)
moon_cord = st_bar_me_moon - st_bar_me_earth
a = st_bar_moon_earth + st_bar_me_earth
sun_cord = st_bar_sun - a
println(sputnik_cord)
sputnik_cord = [8.0,8.0,8.0,8.0,8.0,8.0,8.0]
moon_sputnik = sputnik_cord - moon_cord
sun_sputnic = sputnik_cord - sun_cord
Req = 6378137
J2 = 1.08262668E-3
GMe = 398600.4418E+9
GMm = 4.903E+12
GMs = 1.32712440018E+20
function f(dy,y,p,t)
re2=(y[1]^2 + y[2]^2 + y[3]^2)
re3=re2^(3/2)
rs3 = ((y[1]-sun_cord[1])^2 + (y[2]-sun_cord[2])^2 + (y[3]-sun_cord[3])^2)^(3/2)
rm3 = ((y[1]-moon_cord[1])^2 + (y[2]-moon_cord[2])^2 + (y[3]-moon_cord[3])^2)^(3/2)
w = 1 + 1.5J2*(Req*Req/re2)*(1 - 5y[3]*y[3]/re2)
w2 = 1 + 1.5J2*(Req*Req/re2)*(3 - 5y[3]*y[3]/re2)
dy[1] = y[4]
dy[2] = y[5]
dy[3] = y[6]
dy[4] = -GMe*y[1]*w/re3
dy[5] = -GMe*y[2]*w/re3
dy[6] = -GMe*y[3]*w2/re3
end
function f2(dy,y,p,t)
re2=(y[1]^2 + y[2]^2 + y[3]^2)
re3=re2^(3/2)
rs3 = ((y[1]-sun_cord[1])^2 + (y[2]-sun_cord[2])^2 + (y[3]-sun_cord[3])^2)^(3/2)
rm3 = ((y[1]-moon_cord[1])^2 + (y[2]-moon_cord[2])^2 + (y[3]-moon_cord[3])^2)^(3/2)
w = 1 + 1.5J2*(Req*Req/re2)*(1 - 5y[3]*y[3]/re2)
w2 = 1 + 1.5J2*(Req*Req/re2)*(3 - 5y[3]*y[3]/re2)
dy[1] = y[4]
dy[2] = y[5]
dy[3] = y[6]
dy[4] = -GMe*y[1]*w/re3 - GMm*y[1]/rm3 - GMs*y[1]/rs3
dy[5] = -GMe*y[2]*w/re3 - GMm*y[2]/rm3 - GMs*y[2]/rs3
dy[6] = -GMe*y[3]*w2/re3- GMm*y[3]/rm3 - GMs*y[3]/rs3
end
y0 = sputnik_cord
jd=jd*86400
jd2=jd2*86400
using DifferentialEquations
prob = ODEProblem(f,y0,(jd,jd2))
sol = solve(prob,DP5(),abstol=1e-13,reltol=1e-13)
prob2 = ODEProblem(f2,y0,(jd,jd2))
sol2 = solve(prob2,DP5(),abstol=1e-13,reltol=1e-13)
println("Without SUN and MOON")
println(sol[end])
for i = (1:6)
println(sputnik_cord[i]-sol[end][i])
end
println("With SUN and MOON")
println(sol2[end])
What(except the values) can be a reason of this? It worked well before I added the terms with sun_coords and moon_coords in definition of dy[4], dy[5], dy[6] in function f2(As I suppose the function f1 works correctly).
There are two reasons this could be happening. For one, you could see this error because the model is unstable due to implementation issues. If you accidentally put something in wrong, the solution may be diverging to infinity and as it diverges the time steps shorten and it exists with this error.
Another thing that can happen is that your model might be stiff. This can happen if you have large time scale differences between different components. In that case, DP5(), an explicit Runge-Kutta method, is not an appropriate algorithm for this problem. Instead, you will want to look at something for stiff equations. I would recommend giving Rosenbrock23() a try: it's not the fastest but it's super stable and if the problem is integrable it'll handle it.
That's a very good way to diagnose these issues: try other integrators. Try Rosenbrock23(), CVODE_BDF(), radau(), dopri5(), Vern9(), etc. If none of these are working, then you will have just tested your algorithm with a mixture of the most well-tested algorithms (some of them Julia implementations, but others are just wrappers to standard classic C++ and Fortran methods) and this suggests that the issue is in your model formulation and not a peculiarity of a specific solver on this problem. Since I cannot run your example (you should make your example runnable, i.e. no extra files required, if you want me to test things out), I cannot be sure that your model implementation is correct and this would be a good way to find out.
My guess is that the model you have written down is not a good implementation because of floating point issues.
GMe = 398600.4418E+9
GMm = 4.903E+12
GMs = 1.32712440018E+20
these values have only precision 16 digits less than their prescribed value:
> eps(1.32712440018E+20)
16384.0
> 1.32712440018E+20 + 16383
1.3271244001800002e20
> 1.32712440018E+20 + 16380
1.3271244001800002e20
> 1.32712440018E+20 + 16000
1.3271244001800002e20
Notice the lack of precision below the machine epsilon for this value. Well, you're asking for
sol = solve(prob,DP5(),abstol=1e-13,reltol=1e-13)
precision to 1e-13 when it's difficult to be precise to 1e5 given the size of your constants. You need to adjust your units or utilize BigFloat numbers if you want this kind of precision on this problem. So what's likely going on is that the differential equation solvers are realizing that they are not hitting 1e-13 precision, shrinking the stepsize, and repeating this indefinitely (because they can never hit 1e-13 precision due to the size of the floating point numbers) until the stepsize is too small and it exits. If you change the units so that way the constants are more reasonable in size then you can fix this problem.
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!)
I'm having some problems with my code. Here it is:
lambdaz = 1.2;
n = 24;
mu = 0.00055e9;
lambda = sym('lambda','clear');
W = (((2.*mu)./n.^2)).*((lambda.^n)+(lambdaz.^n)+((lambda.^-n).*(lambdaz.^-n))-3);
dW_dlambda = diff(W, lambda);
W2=(((2.*mu)./n.^2).*(lambda.^n))+(((2.*mu)./n.^2).*(lambdaz.^n))+(((2.*mu)./n.^2).*((lambda.^-n).*(lambdaz.^-n)))-(3.*((2.*mu)./n.^2))
dW2_dlambda=diff(W2,lambda)
x=((((lambda.^2).*(lambdaz))-1).^-1).*(dW_dlambda);
x2=((((lambda.^2).*(lambdaz))-1).^-1).*(dW2_dlambda)
P2 = int(x2,lambda)
P=int(x,lambda);
P=(0:1000:26700)
plot(lambda,P)
When I try to plot lambda against P I get the "conversion to double from sym is not possible" error message. I'm not particularly fantastic at Matlab so any help would be gratefully received!
The plot function only works for numeric inputs. Both lambda and P are symbolic expressions (at least before overwrote P by setting it equal to a vector after the integration) that cannot be directly converted to to floating point. You get the same error if try to something like double(sym('exp(x)')). You have two options. The first is the ezplot function in the Symbolic Toolbox:
...
P = int(x,lambda);
ezplot(P,[-5 5]); % Plot's P from lambda = -5 to lambda = 5
Or you can use the subs function:
...
P = int(x,lambda);
lambda = -5:0.01:5;
plot(lambda,real(subs(P,'lambda',lambda)))
axis([lambda(1) lambda(end) -1e15 1e15])
I used real to suppress a warning for negative values of lambda.