When trying to run the code below in scilab, i get an error on line 6 "inconsistent row/column dimensions" - scilab

Here is the code, Line 6 is where I'm getting the 'inconsistent row/column dimensions' error
T=10:10:500;
k=1.38e-23;
Eg=1.1*1.6e-19;
Nc=5.41e15*(T.^1.5);
Nv=2.23e15*(T.^1.5);
ni=sqrt(Nc*Nv')*exp(-((Eg)/(2*k*T)));
plot2d(T', ni)

Since your operands are vectors you are missing a dot before each element-wise operator (the multiplication before the exponential and the division):
ni=sqrt(Nc*Nv').*exp(-((Eg)./(2*k*T)));

Related

How can we do operations inside indexing operations in R?

For example, let's imagine following vector in R:
a <- 1:8; k <- 2
What I would like to do is getting for example all elements between 2k and 3k, namely:
interesting_elements <- a[2k:3k]
Erreur : unexpected symbol in "test[2k"
interesting_elements <- a[(2k):(3k)]
Erreur : unexpected symbol in "test[2k"
Unfortunately, indexing vectors in such a way in R does not work, and the only way I can do such an operation seems to create a specific variable k' storing result of 2k, and another k'' storing result of 3k.
Is there another way, without creating each time a new variable, for doing operations when indexing?
R does not interpret 2k as scalar multiplication as with other languages. You need to use explicit arithmetic operators.
If you are trying to access the 4 to 6 elements of a then you need to use * and parentheses:
a[(2*k):(3*k)]
[1] 4 5 6
If you leave off the parentheses then the sequence will evaluate first then the multiplication:
2*k:3*k
[1] 8 12
Is the same as
(k:3)*2*k
[1] 8 12

R logical operators - When to use the exclamation point vs the dash (!x and -x)?

For example, consider the following method of splitting data...
set.seed(1)
train=sample(1:nrow(x),size=nrow(x)/2)
test=(-train)
y.test=y[test]
and
set.seed(1)
train=sample(1:nrow(x),size=nrow(x)/2)
test=(!train)
y.test=y[test]
I proceed with the ! operator trying to fit a ridge regression model on the training set, and evaluate its MSE on the test set, using λ = 4.
ridge.mod=glmnet(x[train,],y[train],alpha=0,lambda=grid,thresh=1e-12)
ridge.pred=predict(ridge.mod,s=4,newx=x[test,])
Warning message:
In cbind2(1, newx) :
number of rows of result is not a multiple of vector length (arg 1)
What exactly is happening when I use the !train as opposed to -train? I don't get an error when using the dash operator.
Look at your test variables. In the first case, you're getting a bunch of negative integers, which in R, remove the specified element from a vector:
> (1:10)[-5]
[1] 1 2 3 4 6 7 8 9 10
In the second case, you're treating the numbers as Boolean/logical values, so they're all TRUE, and the ! makes them all FALSE.
> (1:10)[c(FALSE, FALSE)]
integer(0)
not that useful...

Transposing row vector X to get compatible dimensions warning in Scilab

I am learning to use Scilab, I tried plotting a function wich I know have a discontinuity at a certain value but the plot I got had a non expectable behavior so I tried to plot a very well known function "y=1/x".
I created the "x" vector
x=[-10:1:10];
Then created the "y" function
y=1/x;
And then used the plot command
plot(x,y)
I got the following warning
WARNING: Transposing row vector X to get compatible dimensions
And my plot is a straight line, I don't know what I'm doing wrong.
Take care: 1/x compute some thing like 1*pinv(x) not the array [1/x(1),1/x(2),...]. to obtain the previous result use 1 ./x for element wise disvision.
Well, try typing x and y into the consol, to see, how your variables look like:
-->x
x =
column 1 to 12
- 10. - 9. - 8. - 7. - 6. - 5. - 4. - 3. - 2. - 1. 0. 1.
column 13 to 21
2. 3. 4. 5. 6. 7. 8. 9. 10.
-->y
y =
- 0.0129870
- 0.0116883
- 0.0103896
- 0.0090909
- 0.0077922
- 0.0064935
- 0.0051948
- 0.0038961
- 0.0025974
- 0.0012987
0.
0.0012987
0.0025974
0.0038961
0.0051948
0.0064935
0.0077922
0.0090909
0.0103896
0.0116883
0.0129870
So x is a row array, and y is a column array, that's why scilab is complaining about incompatible dimensions. To remove the warning, you must transpose one of your arrays, to make them identical in dimensions. You can do it many places, e.g:
y=1/x';
or
plot(x',y);
or
plot(x,y');
Note: in Scilab 5.4.x and earlier versions there was no such warning, Scilab silently transposed one of the arrays.

How do I prevent the solve() function in R from returning a trivial solution?

I am trying to use solve() in R to find a solution for a 10x10 matrix. Specifically, I am looking for x in Ax=b where b is a ten dimensional 0 vector. When I input solve(A, rep(0,10)), R returns the trivial solution, namely rep(0,10). I also checked -- det(A) is indeed not equal to 0 and thus not singular.
So how can I stop R from returning this result?
Premultiplying both sides of the equation by the inverse of A gives x=A^{-1}b, i.e. on the right hand side we have a zero vector because b is a zero vector. So, that is the only solution.

How can I evaluate an integration to a number?

I have some functions set up like this:
f(x):=1-2**-x$
g(y):=integrate(f(x), x, 0, y)$
and evaluated them:
f(1)$float(%);
g(1)$float(%);
but for g(1), I got a symbolic answer instead of a numerical answer. Using float() was an attempt to get a numerical answer but it just turned all of the terms in the integral into floats.
How can I get g(1) as a number?
Why not just do (by the definition of definite integral):
f(x):=1-2**-x$
gg(x):=''(integrate(f(x), x))$
g(y):=gg(y) - gg(0)$
'' (quote-quote) operator is used to force the evaluation of the :='s right hand side before the assignment.
If you're only interested in a numerical solution, then you could use numerical integration.
For example you could use quad_qag (f(x), x, a, b, key, [epsrel, epsabs, limit]).
I tried:
f(x) := 1-2^(-x);
g(y):= quad_qag(f(x), x, 0, y, 3, epsrel=10d-8)$
g(1);
which returns:
[0.27865247955552,3.093663986714272*10^-15,31,0]
the first entry is the numerical solution,
the second entry is the approximate relative error,
the third entry is the number of iterations required to achieve the solution,
and the last entry is an error code; error codes are
0 if no problems were encountered;
1 if too many sub-intervals were done;
2 if excessive roundoff error is detected;
3 if extremely bad integrand behavior occurs;
6 if the input is invalid.
BTW, the exact solution is 1-1/(2*log(2)) which is approximately 0.27865.

Resources