In [1]:
function f(x, m)
return sin(x)+x^2/m
end
function f_multi(x_array, m)
result = Array(Float64,length(x_array))
for i = 1:length(x_array)
result[i] = sin(x_array[i])+x_array[i]^2/m
end
return result
end
Out[1]:
In [2]:
function countzeros(start, stop, step, func, m)
x = start
counter = -1
last_sign = 0
while x <= stop
if func(x,m) < 0
this_sign = -1
else
this_sign = 1
end
if last_sign != this_sign
last_sign = this_sign
counter = counter + 1
end
x = x + step
end
return counter
end
Out[2]:
In [3]:
a = 0
b = 50
step = .1
m = 100
amount_of_zeros = countzeros(a, b, step, f, m)
println(@sprintf("Die Funktion f mit m=%i hat im Intervall von %.6f nach %.6f %i Nullstellen",m,a,b,amount_of_zeros))
In [4]:
a = 0
b = 50
step = .1
m = 1e6
amount_of_zeros = countzeros(a, b, step, f, m)
println(@sprintf("Die Funktion f mit m=%i hat im Intervall von %.6f nach %.6f %i Nullstellen",m,a,b,amount_of_zeros))
In [5]:
using Plots
a = 0
b = 50
step = .1
m = 1000
x = linspace(a,b,1000)
println(x[1])
y = f_multi(x,m)
plot(x,y)
Out[5]:
In [ ]: