Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Please help with the following question. Thank You!
The problem seems to be flawed. I have made the following calculations:
1, 0, 0.09, 0
0, 1, 0, 0.09
-1.8, 0.9, 1, 0
0.9, -1.8, 0, 1
-i
i
sqrt(30)
-sqrt(30)
-i + 0.09 * sqrt(30) = (a + b * i) * (-i) = b - a * i
i - 0.09 * sqrt(30) = (a + b * i) * i = -b + a * i
-1.8 * i + 0.9 * i + sqrt(30) = -0.9 * i + sqrt(30) = (a + b * i) * sqrt(30)
1.8 * i - 0.9 * i - sqrt(30) = 0.9 * i - sqrt(30) = (a + b * i) * (-sqrt(30))
-----------------------------------------------------------------------------
i - 0.09 * sqrt(30) = a * i - b
0.9 * i - sqrt(30) = -sqrt(30) * a - sqrt(30) * b * i
a = 1
b = 0.09 * sqrt(30)
Check results:
b - a * i = 0.09 * sqrt(30) - i
(a + b * i) * sqrt(30) = sqrt(30) + i * (0.09 * sqrt(30)) * sqrt(30) = sqrt(30) + i * 30 * 0.09 = sqrt(30) + i * 2.7
When I check the results, a and b are calculated for the first row of the eigenvector and naturally, they are correct for the second row as well, as there is only a sign difference. But the imaginary part of the third and fourth row do not comply to the formulas defining the collaboration between eigenvectors and eigenvalues. If I have made a mistake, please, point out, but I have checked my calculations four times.
Related
I need to calculate a double integral on two variables (B0 and B1) in R.
Till now, nothing complicated with the int2 function.
But, my function to integrate includes gamma incomplete function (gammainc in R ) !
The following error message appears :
Error in gammainc(1/eta, lambda * exp(B0 + B1 * z_arm) * tmax^eta) :
Arguments must be of length 1; function is not vectorized.
Any advice to help me ?
tmax = 5
Sig = matrix ( c(0.2, 0, 0, 0.4) , ncol = 2 )
Mu = matrix ( c(1, 0) , ncol = 1 )
eta = 0.5
lambda = 0.8
z_arm = c(rep(0.5,10), rep(1,15))
to.integrate = function(B0, B1)
{
first.int = 1/eta *(lambda * exp(B0 + B1 * z_arm))^(-1/eta)* gammainc(1/eta, lambda * exp(B0 + B1 * z_arm)*tmax^eta)['lowinc']
B = matrix(c(B0, B1), ncol=1)
multi.norm = 1 / (2 * pi * det(Sig)^(1/2)) * exp (- 0.5 * t( B - Mu ) * solve(Sig) * ( B - Mu ) )
return (first.int * multi.norm)
}
int2(to.integrate , a=c(-Inf,-Inf), b=c(Inf,Inf), eps=1.0e-6, max=16, d=5)
Thanks for any help!
When I try to build a model using PKSFC, I get this message:
[... truncated]
R lists three mistakes and a half (the equations end with *_EC).
I guess I can correct the mistake, but I would like to correct them all in one pass.
I guess there are many more (maybe 15?).
The full message:
5: In sfc.check(vn_mod, fill = FALSE) : Equations
57192.4187750356 + POPAGE_1 * (0.5 * (0 - 55335.9694996733)/POPAGE_1 + 0.2 * (POPAGE - POPAGE_1)/POPAGE_1 - 0.2 * (57192.4187750356 - 0.5 * 55335.9694996733 - 0.508679781654891 * POPAGE_1)/POPAGE_1 + ACPOP_EC)
28.7269160437129 * exp(0.548725 * log(0/1.489181) + 0.704327 * log(0/37.4835480991331) - 0.247704 *
(log(28.7269160437129/37.4835480991331 * (1 + R_SCP_1)) - 0.5 *
log(1.489181) - (1 - 0.5) * log(1.52438302758911)) - 0.5 * 0 -
0.0511046682618056 + WP_EC)
1.25809781599844 * exp(0.2 * log(0/1.46079630578026) + 0.8 * log(PPX * 0/(PPX_1 * 1.1362002193817)) - 0.2 * (log(1.25809781599844) - 0.2 * log(1.46079630578026) - (1 - 0.2) * log(PPX_1 * 1.1362002193817)) -
0.00325177611673638 * (T - 2016) * (T <= 2016) - 0.0576138445722962 + PM_EC)
1.30468806319261 * exp(0.7 * log(0/1.46079630578026) + 0.3 * log(PPX * 0/(PPX_1 * 1.1362002193817)) - 0.2 * (log(1.30468806319261) - 0.7 * log(1.46079630578026) - (1 - 0.7) * log(PPX_1 * 1.1362002193817)) -
0.0033333860060119 * (T - 201 [... truncated]
I have an algorithm written as follows but I need to write that code into R. I have included the algorithm and the R code. I am not sure if that is represented well enough. To write the R code in the sequential order is not straight forward. I am sorry for not providing all the values of the variables here. I am not sure of the output yet which is the reason I am unable to show the required. It is more of a theoretical question.
Algorithm
VBDMAX = (va - VG) * 0.79 * (dep / D) ^ -1.21
VBOWMAX = -0.7 * VBDMAX
VBOWX = 0
' SKIP BOW IF -10D<X<15D OR OUTSIDE EDGE OF BARGES
If Y > B / 2 Then GoTo 200
If X < -10 * D Then GoTo 200
If X >= 15 * D Then GoTo 200
VBOWX = X * VBOWMAX / (10 * D) + VBOWMAX
If X <= 0 Then GoTo 200
VBOWX = X * (VBDMAX - VBOWMAX) / (5 * D) + VBOWMAX
If X <= 5 * D Then GoTo 200
VBOWX = -X * VBDMAX / (10 * D) + 15 * VBDMAX / 10
200 ' end bow
This is the R code that I have written
VBDMAX = (va - VG) * 0.79 * (dep / D) ^ -1.21
VBOWMAX = -0.7 * VBDMAX
VBOWX = 0
# SKIP BOW IF -10D<X<15D OR OUTSIDE EDGE OF BARGES
VBOWX <- ifelse ((Y>B/2 | X < -10*D | X>=15*D), 0,X*VBOWMAX/(10*D)+VBOWMAX)
VBOWX <- ifelse (X<=0 , X * (VBDMAX - VBOWMAX) / (5 * D) + VBOWMAX,
ifelse(x <=5*D, -X * VBDMAX / (10 * D) + 15 * VBDMAX / 10))
You can use ifelse constructs but you will need to nest those:
VBDMAX = (va - VG) * 0.79 * (dep / D) ^ -1.21
VBOWMAX = -0.7 * VBDMAX
VBOWX =
ifelse(Y > B / 2 || X < -10 * D || X >= 15 * D,
0,
ifelse(X <= 0,
X * VBOWX / (10 * D) + VBOWMAX,
ifelse(X <= 5 * D,
X * (VBDMAX - VBOWMAX) / (5 * D) + VBOWMAX,
-X * VBDMAX / (10 * D) + 15 * VBDMAX / 10
)
)
)
Understanding your question as how to translate "goto" statements to R, there are the following posibilities (if really needed) besides or in adjunction to the (often more appropriate) if/ifelse constructions as you already did:
a) entire code (for severe errors or if problem is solved): if (condition) stop("explain why...") or stopifnot(condition)
b) from within loops: see next and break
c) from within function: if (condition) return(), stopping the function here
I have a slider that returns values from 0 to 100.
I am using this to control the gain of an oscillator.
When the slider is at 0, I would like the gain to be 0.0
When the slider is at 50, I would like the gain to be 0.1
When the slider is at 100, I would like the gain to be 0.5
So I need to find an equation to get a smooth curve which passes through all of these points.
I've got the following equation which gives an exponential curve and gets the start and end points correct, but I don't know how to force the curve through the middle point. Can anyone help?
function logSlider(position){
var minP = 0;
var maxP = 100;
var minV = Math.log(0.0001);
var maxV = Math.log(0.5);
var scale = (maxV - minV) / (maxP - minP);
return Math.exp(minV + scale*(position-minP));
}
Here's a derivation of the function. All it takes is some algebra.
Model and Constraints
We want an exponential function of the following form, that takes number between 0 and 1:
f(t) = a * bt + c
The function must satisfy these constraints you gave:
f(0) = 0 = a + c
f(1/2) = 0.1 = a * b1/2 + c
f(1) = 0.5 = a * b + c
Solving for a, b, c
Let z2 = b.
a + c = 0
a * z + c = 0.1
a * z * z + c = 0.5
a * z - a = 0.1
a * z * z - a = 0.5
a * z * z - a * z = 0.4
(a * z - a) * z = 0.4
0.1 * z = 0.4
z = 4
b = 16
a * 4 - a = 0.1
a = 0.1 / 3
c = -0.1 / 3
Solution
f(t) = (0.1 / 3) * (16t - 1)
Here's a plot.
If you want to pass in values between 0 and 100, simply divide by 100 first.
Try
y = 0.2*(x/100)^3 + 0.3*(x/100)^2
The general solution for points (50, y_1) and (100, y_2) is
y = 2 (x/100)^3 * ( y_2-4*y_1) + (x/100)^2 * ( 8*y_1 - y_2 )
I am trying to calculate the tangent line (needed for bump mapping) for every vertex in my mesh. The v1, v2 and v3 are the vertices in the triangle and the t1, t2 and t3 are the respective texture coords. From what i understand this should output the tangent line for the three vertices of the triangle.
Vec3f va = Vec3f{vertexData[a * 3 + 0], vertexData[a * 3 + 1], vertexData[a * 3 + 2]};
Vec3f vb = Vec3f{vertexData[b * 3 + 0], vertexData[b * 3 + 1], vertexData[b * 3 + 2]};
Vec3f vc = Vec3f{vertexData[c * 3 + 0], vertexData[c * 3 + 1], vertexData[c * 3 + 2]};
Vec2f ta = (Vec2f){texcoordData[a * 2 + 0],texcoordData[a * 2 + 1]};
Vec2f tb = (Vec2f){texcoordData[b * 2 + 0],texcoordData[b * 2 + 1]};
Vec2f tc = (Vec2f){texcoordData[c * 2 + 0],texcoordData[c * 2 + 1]};
Vec3f v1 = subtractVec3f(vb, va);
Vec3f v2 = subtractVec3f(vc, va);
Vec2f t1 = subtractVec2f(tb, ta);
Vec2f t2 = subtractVec2f(tc, ta);
float coef = 1/(t1.u * t2.v - t1.v * t2.u);
Vec3f tangent = Vec3fMake((t2.v * v1.x - t1.v * v2.x) * coef,
(t2.v * v1.y - t1.v * v2.y) * coef,
(t2.v * v1.z - t1.v * v2.z) * coef);
My problem is that the coef variable is sometimes the nan (not a number) value causing the multiplication to be off. My mesh is not super complex, a simple cylinder, but i would like a universal formula to calculate the tangent line to enable bump mapping on all of my meshes.
coef becomming a NaN indicates some numerical problem with your input data, like degenerate triangles or texture coordinates. Make sure that the expression (t1.u * t2.v - t1.v * t2.u) doesn't (nearly) vanish, i.e. its absolute value is larger than some reasonable threshold value.
A good sanity check is |vb-va|>0 ^ |vc-va|>0, |tb-ta|>0 ^ |tc-ta|>0, |normalized(vb-va) . normalized(vc-va)| < 1 and |normalized(tb-ta) . normalized(tc-ta)| < 1.