Defining derivate of a function - scilab

I'm developing a program to draw speed and power curves as a function of time, my only problem is that i do not know how to translate the equation: dt / dv into a program.
Here is the program:
// Caractéristique du véhicule
Mv = input("Mv = ") // la masse de la voiture
S = input("S = ") // la surface de la voiture
Cx = input("Cx = ") // Le coeficient de pénétration dans l'air
Cr= input ("Cr = ") // Le coeficient de roulement
Dr= input("Dr = ") // Le diametre des roues
r= input(" r = ") // Le rapport de réduction du réducteur
alpha= input(" alpha = ")
nt= input(" nt = ")
omega= input(" omega = ") //La vitesse de rotation (rad/s)
pair= input(" P_air = ") //la masse volumique de l'air en Kg/m^3
g= input(" g = ")
//////////////////////////////
for t=1:195;
if t<11
v(t)=(1.92*t)-224.5;
Pm(t)=v(t)*(Mv*(dv/dt)+(1/2)*pair*
(v(t)^2)*S*Cx+Mv*g*sin(alpha)+Mv*g*Cr*cos(alpha));
I just gave you the first line of the program, just to know how to translate dv / dt into a program.
I will be very grateful if you could help me because I really need it for my project.
Thank you

The nice thing about your velocity function is that it is only dependant on time and is also linear so you can differentiate it fairly easy. That is dv(t)/dt = 1.92. Your power function should look something like this:
Pm(t)=v(t)*(Mv*1.92+(1/2)*pair);

Because your v has a functional form which is easily differentiable, you don't need to construct dv/dt in the program, you already know what it is. For your specific case, you can use 1.92 in place of dv/dt.

Related

Julia - trying to compare two strings

I am trying to compare two strings I got as input, but it printed at me an error like "syntax: unexpected "="".
x = readline()
y = readline()
y = println(cmp(y, "Ciao"))
x = println(cmp(x, "Ciao"))
if x=1
println("Ho fatto un confronto in modo giusto")
elseif x=0
println("No simone, non hai inserito quello che mi aspettavo")
elseif y=1
println("Hai inserito la y e l'ho controllata, il programma funziona")
elseif y=0
println("No simone, non hai inserito quello che mi aspettavo")
else println("Il programma non ha funzionato")
end
return x,y
In Julia, you should use == for comparison: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Numeric-Comparisons
This is different from assignment operator =, so for example
# Assignment
julia> x = 1
1
# Comparison
julia> x == 1
true
Also: println returns nothing so this code still wouldn’t work even with that fix. If you delete the calls to println and fix the assignments to be equality checks, then the code might work.

How to loop through a list of matrices and compute the matrix inverse of each element (lapply)?

Good afternoon !
Under R, i developped the following code :
k=10
w_k=rep(1,k)/k
n_j=rep(0,k)
print(w_k)
data=as.matrix(iris[1:150,-5])
means=sample(1:dim(data)[1],k,replace=FALSE)
mu=as.matrix(iris[means,-5])
sigma=cov(data)
sigma_list=rep(list(sigma),k)
P_Cj_Xi<-function(Xi,mu,sigma_list=sigma_list){
k=length(Xi)
n_j=rep(0,k)
r=lapply(1:k, function(i) r[i]=solve(matrix(unlist(sigma_list[i]),ncol=k)))
# lapply isn't storing the solve(matrix(unlist(sigma_list[i]),ncol=k)) from 1 to k
# try print(solve(matrix(unlist(sigma_list[i]),ncol=k))) , the inverse
# is correctly computed. I need to access each of inverses .
}
# example of run :
P_Cj_Xi(Xi=data[1,],mu,sigma_list=sigma_list)
The sigma_list is a list that contains k=10 of matrices. All of those matrices are initially equal to sigma=cov(data). I need to loop through sigma_list and compute the inverse of each matrix in this list. Those calculated inverse matrices should be stored in another list r.
With the previous code , i'm usually getting this error :
Warning message in r[i] = solve(matrix(unlist(sigma_list[i]), ncol = k)):
"le nombre d'objets à remplacer n'est pas multiple de la taille du remplacement"
Warning message in r[i] = solve(matrix(unlist(sigma_list[i]), ncol = k)):
"le nombre d'objets à remplacer n'est pas multiple de la taille du remplacement"
Warning message in r[i] = solve(matrix(unlist(sigma_list[i]), ncol = k)):
"le nombre d'objets à remplacer n'est pas multiple de la taille du remplacement"
Warning message in r[i] = solve(matrix(unlist(sigma_list[i]), ncol = k)):
"le nombre d'objets à remplacer n'est pas multiple de la taille du remplacement"
I hope my question is clear . Thank you for help in advance !
I should create an empty matrix with r=matrix(NA,length(Xi),length(Xi)) then i could repeate r a number of times =n_clusters
P_Cj_Xi<-function(Xi,mu,sigma_list=sigma_list,n_clusters=k){
k=n_clusters
n_j=rep(0,length(Xi))
r=matrix(NA,length(Xi),length(Xi))
r=rep(list(r),n_clusters )
r=lapply(1:n_clusters , function(i) r[[i]]=solve(matrix(unlist(sigma_list[i]),ncol=length(Xi))))
r
#n_j=sapply(1:k, function(i) -1/2*(Xi-mu[i])%*%r[i]%*%(t(Xi-mu[i])))
#n_j
}
P_Cj_Xi(Xi=data[1,],mu,sigma_list=sigma_list)

I'm trying to graph a function for different values of Chi and Wn?

I'm trying to graph a function for different values of Chi and Wn. Here's the function.
A1=0.1
A2=0.2
A3=0.3 //Valores de chi
A4=0.4
A5=0.5
B1=29
B2=30
B3=65 //valores deW
B4=80
B5=100
C=1
u=linspace(0,600)
t=linspace(0,30)
r=((sqrt(1-A1^2))*B1)
r1=((sqrt(1-A2^2))*B2)
r2=((sqrt(1-A3^2))*B3)
r3=((sqrt(1-A4^2))*B4)
r4=((sqrt(1-A5^2))*B5)
x=(%e^(t-A1*B1))( C*cos(r)*B1*t)' + ((C+(A1*B1*C))/((r)*B1)) *(sin(r)*B1*t)
x1=(%e^(t-A2*B2))( C*cos(r1)*B2*t)' + ((C+(A2*B2*C))/((r1)*B2)) *(sin(r1)*B2*t)
x2=(%e^(t-A3*B3))( C*cos(r2)*B3*t)' + ((C+(A3*B3*C))/((r2)*B3)) *(sin(r2)*B3*t)
x3=(%e^(t-A4*B4))( C*cos(r3)*B4*t)' + ((C+(A4*B4*C))/((r3)*B4)) *(sin(r3)*B4*t)
x4=(%e^(t-A5*B5))*( C*cos(r4)*B5*t)' + ((C+(A5*B5*C))/((r4)*B5)) *(sin(r4)*B5*t)
xlabel("t")
ylabel("x(t)")
title("Grafica de la Ecuación")
plot(t,x,t,x1,t,x2,t,x3,t,x4)
I get Invalid error in line 19
You use too many useless parenthesis (sometimes forget needed ones), use dumb variable names (use the names of variables in the formula) and do not use use dot-prefixed operators. Here is a readable working code to do what you want:
clear
//Valores de zeta
zeta = 0.1:0.1:0.5
//valores de omega
omega = [29 30 65 80 100]
x_0 = 1;
xdot_0 = 1;
t = linspace(0,3,1000)
for i=1:length(zeta)
r = sqrt(1-zeta(i)^2)*omega(i);
x(i,:) = exp(-zeta(i)*omega(i)*t).*(x_0*cos(r*t) + (xdot_0+zeta(i)*omega(i)*x_0)/r*sin(r*t))
end
clf
plot(t,x)
xlabel("t")
ylabel("x(t)")
title("Grafica de la Ecuación")
xstring(0.2,0.5,"$\large x(t)=\exp(-\zeta\omega_n t)\left\{ x_0\cos\left(\sqrt{1-\zeta^2}\omega_n t\right)+\frac{\dot x_0+\zeta\omega_n x_0}{\sqrt{1-\zeta^2}\omega_n} \sin\left(\sqrt{1-\zeta^2}\omega_n t\right)\right\}$")

scilab simple plot instruction returns modulo error

x = [0:0.01:1]; plot(x);
gives :
--> plot(x) à la ligne 39 de la fonction modulo ( C:\Program Files\scilab-6.0.2\modules\elementary_functions\macros\modulo.sci
ligne 54 ) à la ligne 80 de la fonction plot (
C:\PROGRA~1\SCILAB~1.2\modules\graphics\macros\plot.sci ligne 93 )
Indice invalide.
Can anyone be so kind to tell me why scilab doesn't behave correctly ?
thks in advance
I think you have redefined the int function somewhere because line 39 of the modulo function is
i = n - int(n ./ m) .* m
If I enter
int=1:3;
x = [0:0.01:1]; plot(x);
I obtain exactly the same error.
In previous Scilab versions function redefinition throw a warning. But sadly it has been removed....
I can't reproduce this problem, but I recommend that you use pot2d instead. You will lose compatibility with MATLAB plot syntax, though.

rmetrics - test if longitude and latitude coordinates are on land or sea

I have a series of longitudes and latitudes for earthquakes. I would like to be able to split them into ones that are over land and ones that are over sea.
Is there an r function to do that?
It kinda depends on how much work you want to do.
First, grab the "oceans" shapefile from the Natural Earth site: http://www.nacis.org/naturalearth/110m/physical/ne_110m_ocean.zip
Unzip that somewhere (the example below has it on the desktop since I was being lazy), then read it in and use a function from the prevR package (which I've included below since getting prevR to install isn't worth it for just the function you need).
library(sp)
library(rgdal)
require(maptools)
# VERBATIM COPY FROM prevR package. They deserve all the credit for this function
point.in.SpatialPolygons = function(point.x, point.y, SpP){
###############################################################################################
# Cette fonction renvoie pour chaque point defini par le couple (point.x, point.y) T ou F
# si le point est a l'interieur ou non du spatialPolygons SpP
# Un point est considere a l'interieur de N polygons si il est a l'interieur d'au moins
# un polygon non Hole et a l'exterieur de tous les polygons Hole
# Cette foncion est utilisee par toutes les fonctions de lissage (krige , kde, idw) .
# En effet ces fonctions travaillent sur un grid rectangulaire englobant les donnees. En presentation on ne veut que les resulats
# interieurs a la frontiere qui est definie dans l'element SpP (SpP contient boundary).
# Tous les elements du grid hors de la frontiere seront dans les programmes de lissage positonnes a NA
#
###############################################################################################
X = slot(SpP,"polygons")
is.inside = F
for(i in 1:length(X)){
PS = slot(X[[i]],"Polygons")
for(j in 1:length(PS)){
pol.x = slot(PS[[j]],"coords")[,1]
pol.y = slot(PS[[j]],"coords")[,2]
pointsPosition = point.in.polygon(point.x, point.y, pol.x, pol.y)
if(!slot(PS[[j]],"hole")) {
is.inside = is.inside | pointsPosition != 0
}
}
}
is.outsideHole = T
for(i in 1:length(X)){
PS = slot(X[[i]],"Polygons")
for(j in 1:length(PS)){
pol.x = slot(PS[[j]],"coords")[,1]
pol.y = slot(PS[[j]],"coords")[,2]
pointsPosition = point.in.polygon(point.x, point.y, pol.x, pol.y)
if(slot(PS[[j]],"hole")) {
is.outsideHole = is.outsideHole & (pointsPosition == 0 | pointsPosition == 3)
}
}
}
is.inside & is.outsideHole
}
# where is the oceans' shapefile
setwd("~/Desktop/ne_110m_ocean/")
# read in the shapefile; repair=TRUE prbly isn't necessary
# but it doesn't hurt and it's a force of habit for me
oceans <- readShapePoly("ne_110m_ocean.shp", repair=TRUE)
point.in.SpatialPolygons(-105, 45, oceans)
## [1] FALSE
point.in.SpatialPolygons(-135, 30, oceans)
## [1] TRUE
point.in.SpatialPolygons(-75, -25, oceans)
## [1] TRUE
point.in.SpatialPolygons(-45, -15, oceans)
## [1] FALSE
point.in.SpatialPolygons(165, -15, oceans)
## [1] TRUE
point.in.SpatialPolygons(155, 73, oceans)
## [1] TRUE
That's not alot of testing, but you can let me know if it works for your needs.

Resources