After using the 'solve' function on an equation with one variable, it seems like Matlab doesn't like using floating point. So, my answer is
ans = -2515439103678008769411809280/29019457930552314063110978530889-1/232155663444418512504887828247112*13479465975722384794797850090594238631144539220477565900842902305^(1/2)
and I'm not sure what the best way to convert it to an understandable decimal is. The best way I have so far is
eval(char(ans))
(which gives me -0.5002 for those keeping track), but I'm not really happy with that solution.
Can anyone suggest something better?
Well, the very next page I looked at had the answer. It's the 'double' function:
double(ans)
Related
everyone!
I've posted a similar problem, initially on Stackexchange; it was moved here and can be found at the link: Declaring a functional recursive sequence in Matlab I'm trying to do something similar in this post, but I've figured that Matlab isn't the place to do it. I'll have to use Pari-GP; and at this point there's no two ways about it.
This is essentially a coding project I've made for myself; which is to effectively numerically evaluate a certain construction of the Tetration function. I've been able to code it fairly well in MatLab; the trouble is, as we are dealing with large numbers like e^e^e^e^e^e^e; these short circuit in MatLab. Pari-GP has a much better understanding of numbers which would usually cause overflows; and I'm now fully realizing why it's used consistently by the Tetration community.
Nonetheless, the Matlab code works for small numbers and numbers with a niceness to them (well placed imaginary argument). For completeness of this question; the github repository for the matlab code is https://github.com/JmsNxn92/Recursive_Tetration This isn't the code we want though; I've optimized it further; this isn't up to date totally. But for the purpose of this question, it's enough.
Now, I'm not the best programmer. I haven't programmed since '09, maybe; but I still know my way around. But it's more that I have the framework of coding; and less the actual syntax. Imagine being well acquainted with french philosophy and french writing; but stumbling your words when ordering a cafe.
As to that, I'll stop beating around the bush, and get to the question.
If I define a function beta_function in Pari-GP and write it as,
beta_function(z,l,n) =
{
out = 0;
for(i=0,n-1,
out = exp(out)/(exp(l*(n-i-z)) +1));
out;
}
Everything is good, and it works. Now the code for beta_function in MatLab isn't very different. There's nothing more complex being added. As I originally asked for MatLab, I'm asking again for Pari-GP. This is how to code a function tau_K(z,l,n,k); which is perfectly doable. I'm just missing something obvious.
The code for tau_K(z,l,n,k) in MatLab is attached below. A friendly person on here explained how to do this in MatLab; for those of you interested, I wasn't really defining the recursion properly beforehand. Here is the current MatLab code I'm using,
function f = tau_K(z,l,n,k)
if k == 1
f = -log(1+exp(-l*z));
return
end
f = log(1 + tau_K(z+1,l,n,k-1)./beta_function(z+1,l,n)) - log(1+exp(-l*z));
end
The question is simple. How would one define this recursion in Pari-GP; how do you code this in Pari-GP?
Everything seems to be collapsing towards a return value at 0, when I try to directly translate this code. And honest to god; I know it's just because I'm making some syntax error in how I'm calling the output into the next iteration. I've tried everything I could think of. And the tutorials, they don't seem to be helping. I've tried next to everything. And at this point, I know I'm missing something stupid syntactically.
I'm just hoping someone here would be so helpful as to explain this to me like I'm in kindergarten. I've heard that tail recursion is important here. And if so, how would I code this in? Simply add in a variable which keeps track of everything?
Again, thanks if you got this far into the question.
When asking questions, it would help if you would provide expected output for some specified given arguments, otherwise it is hard to test. I don't know MATLAB, but your functions could be written in PARI:
beta_function(z,l,n)={
my(out = 0);
for(i=0,n-1,
out = exp(out)/(exp(l*(n-i-z)) +1));
out;
}
tau_K(z,l,n,k)={
if(k == 1,
-log(1+exp(-l*z)),
log(1 + tau_K(z+1,l,n,k-1)/beta_function(z+1,l,n)) - log(1+exp(-l*z))
)
}
In the beta_function, it is important to put my() around out = 0. This keeps the variable local to the function. Failure to do this, means that out will be a global variable, and many subtle bugs can arise.
PARI is a functional programming language which means you often don't need to assign things explicitly to temporary variables. For example if will return a value and this can be returned from your tau_K function (in your MATLAB code you assign to a temporary variable f, but in PARI this is not necessary).
There are no issues with calling a function recursively. In this case, tau_K can just call itself as needed.
In the MATLAB program you have ./. I don't know what this means - I have replaced by / which is just the normal division operator.
Before running you will need to set some precision for the numeric operations. The easiest way to achieve this is to enter \p100 at the PARI-GP prompt. (or \p1000 if you need a 1000 decimal digits of precision). It is possible to control precision dynamically, if you need some part of the calculation performed at high precision and other parts at a lower precision or if the precision needs to be dependent on n.
The image shows my problem (link)
I want to use that to do a Jacobian for a Multivariable method of aproximation. So There is a way to use that easily? or I have to put manually the partial derivatives like new functions?
I'm sorry for my english.
Firstly, you dont have to use images here, you can easily insert code pieces. As for your problem, i didnt understand it. Im sure you already know that you can define a Jacobian matrix function of 2 variables:
j2_fun(f1,f2):=[[derivative(f1,x),derivative(f1,y)][derivative(f2,x),derivative(f2,y)]]
... and call it later with specific arguments, and evaluate for some x,y:
g1:=x^2+y^2
g2:=x^2-y^2
d2:=j2_fun(g1,g2)
d2|x=3 and y=1
So staring from here, please define more clearly what would you like to do with this.
The solution to Laplace PDE on rectangle is usually written using hyperbolic trig functions. I solve this PDE using Maple. Verified Maple solution is correct. But having hard time figuring how to make its result match the book result.
I tried sol:=convert(rhs(sol),trigh): then simplify(sol,trig); and it become little closer to the book solution, but is still can be more simplified.
Are there any tricks to do this?
Here is MWE
restart;
interface(showassumed=0):
pde:=diff(u(x,y),x$2)+diff(u(x,y),y$2)=0:
bc:=u(0,y)=0,u(a,y)=f(y),u(x,0)=0,u(x,b)=0:
sol:=pdsolve([pde,bc],u(x,y)) assuming(0<=x and x<=a and 0<=y and y<=b):
sol:=subs(infinity=20,sol);
Which gives
The above is same as the following, which I am trying to convert the above to
textbookU:= Sum(2*sin(n*Pi*y/b)*(Int(sin(n*Pi*y/b)*f(y),
y = 0 .. b))*sinh(n*Pi*x/b)/(b*sinh(n*Pi*a/b)), n = 1 .. 20);
The above are the same. I checked few points, and they give same answer. They must be the same, as the above textbook solution is correct, and I am assuming Maple solution is correct.
Now I tried to convert Maple sol to the above as follows
sol:=convert(rhs(sol),trigh):
simplify(sol,trig);
May be someone knows a better way to obtain the textbook solution form, starting from the Maple solution above.
Using Maple 2017.3 on windows
After the convert you can first expand it, to then simplify it again:
s := convert(sol, trigh):
s := expand(s):
simplify(s);
which gives:
I want to do a numerical integration of the following equation in Matlab.
For simplicity, lets pretend that only the second fraction of the equation is there.
And this is what I have tried so far:
for lambda=280:2500
numerator(lambda) = trapz(Einc(lambda:lambda+1)*St(lambda));
denominator(lambda) = trapz(Eref(lambda:lambda+1)*St(lambda));
end
Fraction= nansum(numerator)/ nansum(denominator);
St is not defined in the entire 280-2500 range, that is why I use nansum at the end. But overall I feel that what I’m doing is wrong and that there should be a much more normal and correct way of calculating this, yet I have tried googling this and asked on Mathworks forum as well but no one answers me even though this should be a pretty common thing to calculate… Could someone please help me how do write this formula?
I have defined the following ODE
syms R1 C1 vc0 Vin
Vc_ode = 'Dvc+vc/(R1*C1)=(Vin)/(R1*C1)';
Vc=dsolve(Vc_ode,'vc(0)=vc0','t');
and the solution I receive is
Vin - (Vin - vc0)/exp(t/(C1*R1))
while solving manually I get
Vin +vc0*exp(-t/(C1*R1))
both are correct solutions, but is there a way to reach my desired solution?
I think the practical answer would be: No, you cannot let MATLAB reach your desired solution.
When looking at the dsolve input there is no option to specify what the output should look like. It is just a guess but this may be because it is hard to translate your desired style into code.
The only thing that might make a difference is the way you write the input formula but I would suspect it is not going to make much difference.
On the other hand, the academic answer would be: Everything is possible, you may need to make your own dsolve function though.
The problem is that the manual solution vc(t) = Vin +vc0*exp(-t/(C1*R1)) is incorrect. That solutions has vc(0) = Vin + vc0 which is not equal to vc0, so this is why your solutions differ. There is a theorem that states that a first order linear ODE with a initial condition like vc(t_0) = ... has exactly one solution. I suggest that you carefully go through you steps.