Define a piecewise function in Scilab given the variable as a vector - scilab

Hi I have the following code in Scilab:
>Tc=0;
>Tm=1;
>Tf=1800;
>t=(Tc:1:Tf)';
where t is a vector of 1800 components.
And I am asked to do a piecewise function that satisfies certain conditions,
My first try was to do something on the line of
> function vg=simula_vg(t,Tcg,Tfg,Ag)
> if (t<Tcg | t>Tfg) then
> vg=0;
> else
> vg=Ag*Ag*(1-cos(2*%pi*(t-Tcg)/(Tfg-Tcg)));
>end
>endfunction
But it doesnt work as I am asking it to compare vector and scalars.
Then I tried to write this
>for i=[Tc:1:Tf]
>function vg=simula_vg(t,Tcg,Tfg,Ag)
> vg(t<Tcg)=0
>vg(t>Tfg)=0
>vg((Tcg<=t)&(t<=Tfg))=sin(t(i))
>endfunction
>end
But I doesnt work either and I have run out of ideas, is there anything else I can do? All the variables are well defined
>vm=10;
>Ag=2;
>Tcg=200;
>Tfg=400;
>Ar=2;
>Tcr=1000;
>Tfr=1500;
>As=2;
>fs=0.0008;
>h=20;
>d=0.6;
There are more because there are more functions similar to that one that I have to define and I dont know how. Any suggestions on how to do it?

You can do it like this, where the zero values are defined afterwards:
function vg=simula_vg(t,Tcg,Tfg,Ag)
vg=Ag*Ag*(1-cos(2*%pi*(t-Tcg)/(Tfg-Tcg)));
vg(t<Tcg|t>Tfg)=0;
endfunction
Ag=2;
Tcg=200;
Tfg=400;
Tc=0;
Tm=1;
Tf=1800;
t=Tc:1:Tf;
vg = simula_vg(t,Tcg,Tfg,Ag);
plot(t,vg)

Related

R function to check each element and its related children elements to add a result to a list

Suppose we have given dataframe in R. By 0--7, it means it is taking integer values from 0-7 i.e. 0,1,2,3,4,5,6,7.
I am interested in making a function such that
If a[1,1]>alpha, it goes and checks its children i.e. 0--7 consists of a[1,2] and a[2,2].
So,
{a[2,1]>alpha
{a[4,1]>alpha
{a[5,1]>alpha
ps=list.append(0)
else ps=list.append(1)
}}}
Here, alpha is a a threshold. The ps is appended from values of 0 to 15 based on this criteria.
My code is
{for (i in 1:2)
{ if (a[j,i]>alpha)
{if (i%%2==1}
{j=j*2
if (a[j,i]>alpha
###here i want to go recursively i think and where and how should i add append values to the list
if a[j,i+1]>alpha}
if{i%%2==0}
{}
}}
I am stuck and confused at the same time. Any help or advices would be greatly appreciated.
Thanks

R error: dims do not match the length of an object

I am currently trying to run some code (if you need to know the purpose to help me, ask me, but I'm trying to keep this question short). This is the code:
par<-c(a=.5,b=rep(1.3,4))
est<-rep(TRUE,length(par))
ncat<-5
Theta<-matrix(c(-6,-5.8,-5.6,-5.4,-5.2,-5,-4.8,-4.6,-4.4,-4.2,-4,-3.8,-3.6,-3.4,-3.2,-3,-2.8,-2.6,-2.4,-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5,5.2,5.4,5.6,5.8,6))
p.grm<-function(par,Theta,ncat){
a<-par[1]
b<-par[2:length(par)]
z<-matrix(0,nrow(Theta),ncat)
y<-matrix(0,nrow(Theta),ncat)
y[,1]<-1
for(i in 1:ncat-1){
y[,i+1]<-(exp(a*(Theta-b[i])))/(1+exp(a*(Theta-b[i])))
}
for(i in 1:ncat-1){
z[,i]<-y[,i]-y[,i+1]
}
z[,ncat]<-y[,ncat]
z
}
However, when I try to run the code:
p.grm(par=par,Theta=Theta,ncat=ncat)
I get the following error:
Error: dims [product 61] do not match the length of object [0]
Traceback tells me that the error is occurring in the first for loop in the line:
y[,i+1]<-(exp(a*(Theta-b[i])))/(1+exp(a*(Theta-b[i])))
Could someone point me to what I'm doing wrong? When I try to run this code step by step outside of the custom p.grm function, everything seems to work fine.
It is a common mistake. When you write the for loop and you want it from 1 to ncat -1 remember to write it as for (i in 1:(ncat-1)) instead of for(i in 1:ncat-1) they are completly different.
You may also add to the function something to return return(z). Here it is the corrected code:
par<-c(a=.5,b=rep(1.3,4))
est<-rep(TRUE,length(par))
ncat<-5
Theta<-matrix(c(-6,-5.8,-5.6,-5.4,-5.2,-5,-4.8,-4.6,-4.4,-4.2,-4,-3.8,-3.6,-3.4,-3.2,-3,-2.8,-2.6,-2.4,-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5,5.2,5.4,5.6,5.8,6))
p.grm<-function(par,Theta,ncat){
a<-par[1]
b<-par[2:length(par)]
z<-matrix(0,nrow(Theta),ncat)
y<-matrix(0,nrow(Theta),ncat)
y[,1]<-1
for(i in 1:(ncat-1)){
y[,i+1]<-(exp(a*(Theta-b[i])))/(1+exp(a*(Theta-b[i])))
}
for(i in 1:(ncat-1)){
z[,i]<-y[,i]-y[,i+1]
}
z[,ncat]<-y[,ncat]
return(z)
}
p.grm(par=par,Theta=Theta,ncat=ncat)

Compare each member of a vector with its number

The problem might be simple, but I cant solve it:
I got numeric vector, i need to compare its maximum/2 with each member except its maximum and if all these comparisons are FALSE , a=a+1.
I got these:
comp=c(6.674971, 11.208241, 18.296459, 5.165752, 123.000000)
a=0
if (max(comp)/2<comp[comp < max(comp)]){
a=a+1
}
a
Thank you in advance.
The condition is true if :
sum(2*comp > max(comp)) ==1
In this case , I would write it like this without using ìf:
a <- (sum(2*comp > max(comp)) ==1) + 1
I don't know if you want to add 1 every time you find a item bigger than max/2, which is what I am showing you. In case you want all to be false, add this line at the end.
if (a>0)
a=1;
I think this will work, haven't compiled it because here I don't have R compiler, but this is the idea:
comp=c(6.674971, 11.208241, 18.296459, 5.165752, 123.000000)
a=0
temp=comp/max(comp)
for (n in 1:length(comp))
{ if ((temp(i)>max(comp)/2)&&(temp(i)!1))
a=a+1
}
Hope it helps.

Warning meassage: number of items to replace is not a multiple of replacement length

I got warnings when running this code.
For example, when I put
tm1<- summary(tmfit)[c(4,8,9)],
I can get the result, but I need to run this code for each $i$.
Why do I get this error?
Is there any way to do this instead of via a for loop?
Specifically, I have many regressants ($y$) with the same two regressors ($x$'s).
How I can get these results of regression analysis(to make some comparisons)?
dreg=read.csv("dayreg.csv")
fundr=read.csv("fundreturnday.csv")
num=ncol(fundr)
exr=dreg[,2]
tm=dreg[,4]
for(i in 2:num)
{
tmfit=lm(fundr[,i]~exr+tm)
tm1[i]<- summary(tmfit)[c(4,8,9)]
}
Any help is highly appreciated
Try storing your result into a list instead of a vector.
dreg=read.csv("dayreg.csv")
fundr=read.csv("fundreturnday.csv")
num=ncol(fundr)
exr=dreg[,2]
tm = list()
for(i in 2:num)
{
tmfit=lm(fundr[,i]~exr+tm)
tm1[[i]]<- summary(tmfit)[c(4,8,9)]
}
You can look at an element in the list like so
tm1[[2]]

Use for loop with if else statement in R

I am trying to create a for loop with an if else statement. My code looks the following:
for(i in 1:length(assignmentlist[,1]))
{if assignmentlist$Approve[i]=="1"
{ApproveAssignment(assignments=assignmentlist$AssignmentId[i],sandbox=T)}
else {RejectAssignment(assignments=assignmentlist$AssignmentId[i],sandbox=T)}}
whereas the "assignmentlist" looks like the following
> assignmentlist
AssignmentId Approve
1 5135 1
2 8963 0
3 6823 0
4 3287 1
Basically I would like to execute the "ApproveAssignment" function for all the entries that have a "1" in the "Approve" collumn. The problem is, that I would like to use the same index (the same i) inside the ApproveAssignment function. Unfortunately, this does not seem to work. Does anyone has a gentle way to avoid this problem?
Edit: The Approve Assignment function is a function that approves a certain assignment of Mechanical Turk over an API and is part of the MTurkR package
Any help yould be appreciated very much!
I don't get the point because the "i" of youy loop can be directly used in your function:
ApproveAssignment <- function(assignments=NULL, sandbox=NULL) cat(i, "was approved\n")
RejectAssignment <- function(assignments=NULL, sandbox=NULL) cat(i, "was rejected\n")
for(i in 1:length(assignmentlist[,1])){
if (assignmentlist$Approve[i]=="1")
ApproveAssignment(assignments=assignmentlist$AssignmentId[i],sandbox=T)
else
RejectAssignment(assignments=assignmentlist$AssignmentId[i],sandbox=T)
}
If, which I assume given I you want to use it inside it, you are the author of ApproveAssignment, you should hand the index over to the function as an additional argument.
ApproveAssignment <- function(assignments, sandbox, index) { ... }
ApproveAssignment and RejectAssignment internally loop through a vector of AssignmentIds to make an assignment-specific call to the API, so all you have to do is feed it a vector of AssignmentIds, no need for the loop or any of the conditionals.
ApproveAssignment(assignments=assignmentlist$AssignmentId[assignmentlist$Approve==1],sandbox=T)
RejectAssignment(assignments=assignmentlist$AssignmentId[!assignmentlist$Approve==1],sandbox=T)

Resources