Trying to translate Matlab code to Julia - need help #50640

manuelcaeiro
Wed, 26 Jul 2023 10:52:43 +0000
General Usage

I’m trying to translate the following code in Matlab:

e = 1.05;          %Eccentricity
 a = -147562.72;    %km
 b = abs(a)*sqrt(e.^2 -1);      %Equation for Semi-Minor Axis, b

 x1 = linspace(   a, 2*a, 1E3);         %From 0 to a, Upper

 ytop =  b*sqrt(x1.^2/a^2-1);  %Upper Hyperbola Part
 ybot = -b*sqrt(x1.^2/a^2-1);  %Lower Hyperbola Part

to Julia.

Everything seems to go well until ytop. Here’s my code up to that point:

 ecc=1.05; a=-14; b=abs(a)*sqrt(ecc.^2 -1)

x1=LinRange(a, 2*a, 100)
100-element LinRange{Float64, Int64}:
 -14.0,-14.1414,-14.2828,-14.4242,-14.5657,…,-27.5758,-27.7172,-27.8586,-28.0

but then:

 ytop=b*sqrt(x1.^2 / a^2 - 1)
 ERROR: MethodError: no method matching -(::Vector{Float64}, ::Int64)
 For element-wise subtraction, use broadcasting with dot syntax: array .- scalar
 Closest candidates are:
  -(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at ~/julia-1.7.1/share/julia base/int.jl:86
  -(::Rational, ::Integer) at ~/julia-1.7.1/share/julia/base/rational.jl:311
  -(::T, ::Integer) where T<:AbstractChar at ~/julia-1.7.1/share/julia/base/char.jl:227
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[41]:1

I went through Julia and Matlab docs trying to understand the Error message (making several changes to the code, trying to match the types, for example), but I couldn’t do it.

Please forgive me my probable ignorance (both of Julia and Matlab), and be willing to help me please.

15 posts - 9 participants