I have an equation which goes like this:
(a/k^2)-((w^2-b)/(w^4-k^2*c*w^2-d))-((e*w^2-f)/(w^4-k^2*g*w^2-h)) (I don’t know how to put this in equation style).
Here a, b, c, d, e, f, g, h are constants. I want to vary k from say .6 to 10 with 0.1 interval and to find w. There are 2 ways to do this in MATLAB.
One way is convert this equation into equation of the form (something)w^8-(something else)w^6....-(something else again)w^0=0, and then make use of command 'roots' in MATLAB.
Another way is that defining symbolic functions and then executing the program. When you are using the this method, you may not need simplify the expression any further, you can just put it in first form itself.
I am giving the programs to do that in both ways:
clear all; clc;
q=[t8 t7 t6 t5 t4 t3 t2 t1 t0];
dat1=[krho1 r_real(:,1) r_real(:,2) r_real(:,3) r_real(:,4) r_real(:,5) r_real(:,6) r_real(:,7) r_real(:,8)];
fprintf(fid1,'krho\t RR1\t RR2\t RR3\t RR4\t RR5\t RR6\t RR7\t RR8\t \r');
fprintf(fid1,'%6.4f %7.10f %7.10f %7.10f %7.10f %7.10f %7.10f %7.10f %7.10f \n',dat1');
plot(krho1, r_real(:,1),krho1, r_real(:,2),krho1, r_real(:,3),krho1, r_real(:,4),krho1, r_real(:,5),krho1, r_real(:,6),krho1, r_real(:,7),krho1, r_real(:,8))
You can comment second section, run the program, and then afterwards comment first section and run the program.
First section (%% for varying k using roots) uses roots command and second section (%% for varying k using solve) uses symbolic and solve. My question are as follows:
- You can see that first section plots are coming in a short time, where the second one is taking a greater time. Is there any way to increase the speed?
- The plots of both the section seems very different, and you may be forced to believe that I have made mistakes while carrying out the calculation from (a/k^2)-((w^2-b)/(w^4-k^2*c*w^2-d))-((e*w^2-f)/(w^4-k^2*g*w^2-h)) to (something)w^8-(something else)w^6....-(something else again)w^0. I can assure you that I have put it correctly. You can see what really happens, if you look for any particular value of krho in both the dat file (stack_using_roots and stack_using_solve). For, lets say, krho=3.6, the roots is the same in both the dat files, but the way in which it is 'written' is not in a proper way. That is why the plots looks awkward. In short, while using 'roots' command, the solutions are given in a orderd format, on the other hand while using 'solve', it is getting shifted randomly. What is really happening? Is there any way to get around this problem?
- I have ran the program with
- syms w along with n(i,:)=double(solve(first-second-third==0, w));
- syms w k along with n(i,:)=double(solve(first-second-third==0, w));
- syms w k along with n(i,:)=double(solve(first-second-third, w));
In all these 3 cases, results seem to be same. Then what is the thing that we have to define as symbolic? And when do we use and do not use the expression '==0'?