I am calling the function mnewton(0=expr, alpha, %pi/4) to get the root of a rather complex equation expr.
%(i1) mnewton(0=expr, alpha, %pi/4)
%(o1) [alpha=0.678193754078621]
I need to apply another function to this result (e.g. sin) and then want to plot it. Just linking the functions does not work:
%(i2) sin(mnewton(0=expr, alpha, %pi/4)[1])
%(o2) sin(alpha=0.678193754078621)
This is because the expression alpha=0.678193754078621 is not a number. How do I convert alpha=0.678193754078621 to just 0.678193754078621?
I can't just copy the numerical value and add it manually as I want to plot this and my expr will have a different root for each y.
The function rhs(expr) does exactly that.
Check the manual for more information on this.
Related
I am trying to take the derivative of a function that includes a scalar function of a vector and the vector itself. A simpler example of this is:
D[ A[b[t]]*b[t]/(b[t].b[t]), t]
where b[t] is a 3-vector and A[b[t]] is a scalar function. I get nonsense back out, since Mathematica isn't properly defining A[b[t]] to be a scalar.
I've tried using $Assumptions = {(b[t]) \[Element] Vectors[3, Reals], (M[b[t]] | t) \[Element] Reals} and this doesn't seem to help.
Any tips?
Edit to add more detail:
In that example case, I should get:
A/(b[t].b[t]) * (2(b[t].b'[t])b[t]/(b[t].b[t])^2 - b'[t])
- D[A,b]*1/(b[t].b[t])^(3/2) * (b[t].b'[t])b[t]
Where ' denotes a derivative with respect to t.
Mathematica gives everything correctly except the last term. The last term is instead:
-((b[t] b'[t] M'[b[t]])/b[t].b[t])
In Octave I defined a function in a separate file square.m
function y = square(x)
y = x^2;
endfunction
In other file script.m I have
disp("Hello World 2");
fplot( #(x) square(x),[-1 1])
And I get
error: for x^A, A must be a square matrix. Use .^ for elementwise power.
Also if I try
y = x.^2;
inside the function I get the exact same message
The reason you're getting that error is because fplot is passing the range you specified all at once as a vector, treating your function as a vectorised function, expecting a vector input and returning a vector output.
You can confirm this by turning "debug on error" to true, by doing debug_on_error(true), run the offending line, and inspect x.
Therefore, inside your function, things go wrong, because you're trying to get the square of a vector, which is an illegal operation (mathematically speaking).
Converting your function to y = x.^2 should work in this case, because you'd be converting each element of the vector to its square, which is what you want. But obviously, simply changing ^ to .^ might not work for every problem.
In general, it's better to create your own 'range' and 'outputs' and plot them directly using plot; this gives you far more control, and you can inspect the inputs and outputs first to ensure you're plotting what you think you're plotting.
Welcome to StackOverflow!
I have just tried your code on https://octave-online.net/ (no need to create an account nor even the files).
The second version works "as expected": y = x .^ 2; inside the function.
Make sure you saved the file after the modification?
I'm relatively new to Scilab and I would like to find the indices of a number in my matrix.
I have defined my number as maximal deformation (MaxEYY) and on displaying it, it is correct (I can double check in my *.csv file). However, when I want to know exactly where this number lies in my matrix using the find command, only (1,1) is returned, but I know for a fact that this number is located at (4,8).
My matrix is not huge (4x18) and I know that this number only occurs once. On opening the *.csv file, I removed the headers so there is no text.
Can anyone help me with this?
N=csvRead("file.csv",",",".",[],[],[],[],1)
EYY=N(:,8);
MaxEYY=max(EYY);
MinEYY=min(EYY);
[a,b]=find(MaxEYY);
disp([a,b]);
First, you need to understand how find() works: it looks for values of true or false in a matrix. So if you want to find a certain value in it, you should do find(value == matrix).
Then, notice that in your code, you are applying find() to MaxEYY, which is a single value, that is, a scalar, a 1-by-1 matrix. When you do that, you can only get (1,1) or [] as results.
Now, combining these two informations, this what you should've done:
[a, b] = find(EYY == MaxEYY);
Also, there is a quicker way to get this indices: max() can also return the indices of the maximum value by doing
[MaxEYY, inds] = max(EYY);
And the same goes for min().
I can get maxima to solve an equation but can't figure out why it won't show it's numerical value without typing the extra command/step of float(%). Is there away to automatically convert a solved variable to a numerical format.
Example of equation below:
kill(all); alpha:float(.0014931); endfreq:50; dursec:1200; solve(alpha=log(startfreq/endfreq)/dursec,float(startfreq));
what comes back is
startfreq=50%e(44793/25000)
I would like it to say 299.988 instead
Well, Maxima prefers exact results (i.e., integers, rational numbers, and symbolic constants) instead of inexact (i.e., float and bigfloat numbers). If you want to work only with numerical solutions, take a look at find_root. E.g.:
(%i1) [alpha, endfreq, dursec] : [0.0014931, 50, 1200] $
(%i2) find_root (alpha = log(startfreq / endfreq)/dursec, startfreq, 1, 500);
(%o2) 299.9881594652534
Note that to use find_root you must know an interval (here 1 to 500) which contains a root of the equation.
I need to write my own function which has the form f(x,y)=Integrate(g(x,y,z),z from 0 to inf). so the code I used was:
function y=f(x,y)
g=#(z)exp(-z.^2)./(z.^x).*(z.^2+y.^2).^(x/2);% as a function of x,y and z
y=quadgk(g,0,inf)
and if I call it for a single value like f(x0,y0), it works but if I try to calculate something like f([1:10],y0), then the error message says that there is something wrong with the times and dimension. In principle I can use for loops but then my code slows down and takes forever. Is there any help I can get from you guys? or references?
I'm trying to avoid the for loop since in matlab it's much faster to use matrix computation than to use for loop. I wonder if there is any trick that I can take advantage of this feature.
Thanks for any help in advance,
Lynn
Perhaps you can try to transpose the interval, creating row based values instead of column based f([1:10]',y0). Otherwise something in your function might be wrong, for example to get x^y to work with lists as input, you have to prefix with a dot x.^y. The same for mulitply and division I think..
If loop is no problem for you, you should do something like:
function y2=f(x,y)
y2=zeros(size(x));
for n=1:numel(x)
g=#(z)exp(-z.^2)./(z.^x(n)).*(z.^2+y.^2).^(x(n)/2);% as a function of x,y and z
y2(n)=quadgk(g,0,inf)
end
The problem here is that quadk itself uses vectors as argument for g. Then you have in g somethink like z.^x, which is the power of two vectors that is only defined if z and x have the same dimension. But this is not what you want.
I assume that you want to evaluate the function for all arguments in x and that the output vector has the same dimension as x. But this does not seem to be possible since even this simple example
g=#(x)[x;x.^2]
quad(g,0,1)
does not work:
Error using quad (line 79)
The integrand function must return an output vector of the same length as the
input vector.
A similar error shows when using quadgk. The documentation also says that this routine works only for scalar functions and this is not surprising since an adaptive quadrature rule would in general use different points for each function to evaluate the integral.
You have to use quadvinstead, which can integrate vector valued functions. But this gives wrong results since your function is integrated in the interval [0,\infty).