Matlab Transform old school min find into fminsearch - algorithm

My code is the following. I was told fminsearch would solve this faster. I checked the docs and tutorials but I'm still in the dark. How would you implement fminsearch here? Thanks in advance.
MIN=1e10;
up_vec= u_min1+ ku*lambda;
vp_vec= v_min1+ kv*lambda;
wp_vec= w_min1+ kw*lambda;
%%the loop
for i_up=1:length(up_vec)
for i_vp=1:length(vp_vec)
for i_wp=1:length(wp_vec)
Jp(i_up,i_vp,i_wp)=norm(p- (A\[up_vec(i_up);vp_vec(i_vp);wp_vec(i_wp)]).* ...
[exp(-1i*2*pi/lambda*up_vec(i_up));...
exp(-1i*2*pi/lambda*vp_vec(i_vp));...
exp(-1i*2*pi/lambda*wp_vec(i_wp))]);
if Jp(i_up,i_vp,i_wp) < MIN
MIN=Jp(i_up,i_vp,i_wp);
ind_umin = i_up;
ind_vmin = i_vp;
ind_wmin = i_wp;
up_vec_min=up_vec;
vp_vec_min=vp_vec;
wp_vec_min=wp_vec;
pp_min=pp;
end
end
end
end

You need to define your objective function and then use fminsearch. for instance:
funJp = #(u,v,w)(norm(p- (A\[u;v;w]).* ...
[exp(-1i*2*pi/lambda*u);...
exp(-1i*2*pi/lambda*v);...
exp(-1i*2*pi/lambda*w)]));
x = fminsearch(funJp,[umin_1,vmin_1,wmin_1]);

Related

Julia Set - problem with own Mathematica implementation

I've been working on my own Julia Set Plot Implementation. I don't want to use JuliaSetPlot, (however I'm eager to use JuliaSetIterationPoints and JuliaSetCount, I just don't really know how).
I've come up with something like this, but I have a problem, I have no idea what is wrong and why it won't work.
Can anyone help?
'''mathematica
firstFun= Function[ {Typed[pixel0, "ComplexReal64"]},
Module[{i = 1, maksi=100, pixel = pixel0},
While[i < maksi && (Abs[pixel])^2 < 2,
temp = (Re[pixel])^2 - (Im[pixel])^2
Re[pixel] = 2 * Re[pixel] * Im[pixel] - 0.8\[Iota] * Im[pixel0]
Im[pixel] = temp - 0.8\[Iota]* Re[pixel0];
i++ ];
i]];
'''
my code
This
firstFun=Function[{Typed[pixel0,"ComplexReal64"]},
Module[{i=1,maksi=100,pixel=pixel0},
While[i<maksi&&Abs[pixel]^2<2,
pixel=2*Re[pixel]*Im[pixel]-0.8*I*Im[pixel0]+
I*(Re[pixel]^2-Im[pixel]^2-0.8*I*Re[pixel0]);
i++];
i]];
compFun[c_]=FunctionCompile[firstFun]
compiles without any compile-time error messages.
If I haven't made a mistake then I think your pixel calculation can be simplified to
pixel=I*Conjugate[pixel]^2+0.8*Conjugate[pixel0]
Please test all this very carefully to make certain that it is correct.

How to set discount value in Quick Book?

I am trying to apply discount value in Quick book but for some reason discount does not apply in Quickbook. Can you please guide me how can i resolve this issue?
https://github.com/ruckus/quickbooks-ruby/issues/267
Discount value code:
discount_amount = discount_value
discount_line_item = Quickbooks::Model::InvoiceLineItem.new
discount_line_item.amount = discount_amount#149
discount_line_item.discount_item! do |detail|
detail.discount_account_id = 48
end
Discount percentage code:
discount_line_item = Quickbooks::Model::InvoiceLineItem.new
discount_line_item.amount = discount_percentage_value#149
discount_line_item.discount_item! do |detail|
detail.discount_percent = discount_percentage_value#60
detail.percent_based = percent_based
detail.discount_account_id = 48
end
invoice.line_items << discount_line_item
I did a google search and fount this link that may be useful in solving your issue. Otherwise, there's no way we can help you; is the application timing out, is the data not being posted to quickbooks, is there an error in your log file?

Time of execution in Python 3.3

I read this topic because I forget a method I found in the net few month ago, and I don't know why I can't find it today, it was very simple and works good but...
So I tried one method but I think it doesn't work good or maybe my computer which is 5 years old is better than today's computer...
import time
debut=time.clock()
def t(n):
aaa=[]
b=n-1
c=0
if n==0 or n==1:
return 1
else:
while n != 1:
if n % 2==0:
n=n//2
aaa.append(n)
else:
n = n+b
aaa.append(n)
return [b,b+1]+aaa, len(aaa)+2
fin=time.clock()
print(t(100000),fin-debut)
For n=10.000.000 i can count in my head approx 5 secondes and computer always return 3.956927685067058e-06 ... can someone explain me ?
And the method I found, used this from time import perf_counter as pc
And I had to return print(pc()-t)
If someone can enlighten me because i really don't remember the method.
Thank you in advance
Look at the timeit module, https://docs.python.org/3.0/library/timeit.html.
you would set yours something like...
time = Timer( "t(100000)", "from __main__ import t")
print("time: ", timer.timeit(number=1000))
You are measuring the time it takes to define the function.
This will measure the execution of the function:
import time
def t(n):
aaa=[]
b=n-1
c=0
if n==0 or n==1:
return 1
else:
while n != 1:
if n % 2==0:
n=n//2
aaa.append(n)
else:
n = n+b
aaa.append(n)
return [b,b+1]+aaa, len(aaa)+2
start = time.time()
value = t(100000)
end = time.time()
duration = end - start
print(value, duration)

Dealing with under flow while calculating GMM parameters using EM

I am currently runnuing training in matlab on a matrix of logspecrum samples I am constantly dealing with underflow problems.I understood that I need to work with log's in order to deal with underflowing.
I am still strugling with uderflow though , when i calculate the mean (mue) bucause it is negetive i cant work with logs so i need the real values that underflow.
These are equasions i am working with:
In MATLAB code i calulate log_tau in oreder avoid underflow but when calulating mue i need exp(log(tau)) which goes to zero.
I am attaching relevent MATLAB code
**in the code i called the variable alpha is tau ...
for i = 1 : 50
log_c = Logsum(log_alpha,1) - log(N);
c = exp(log_c);
mue = DataMat*alpha./(repmat(exp(Logsum(log_alpha,1)),FrameSize,1));
log_abs_mue = log(abs(mue));
log_SigmaSqr = log((DataMat.^2)*alpha) - repmat(Logsum(log_alpha,1),FrameSize,1) - 2*log_abs_mue;
SigmaSqr = exp(log_SigmaSqr);
for j=1:N
rep_DataMat(:,:,j) = repmat(DataMat(:,j),1,M);
log_gamma(j,:) = log_c - 0.5*(FrameSize*log(2*pi)+sum(log_SigmaSqr)) + sum((rep_DataMat(:,:,j) - mue).^2./(2*SigmaSqr));
end
log_alpha = log_gamma - repmat(Logsum(log_gamma,2),1,M);
alpha = exp(log_alpha);
end
c = exp(log_c);
SigmaSqr = exp(log_SigmaSqr);
does any one see how i can avoid this? or what needs to be fixed in code?
What i did was add this line to the MATLAB code:
mue(isnan(mue))=0; %fix 0/0 problem
and this one:
SigmaSqr(SigmaSqr==0)=1;%fix if mue_k = x_k
not sure if this is the best solution but is seems to work...
any have a better idea?

I have to run this script multiple times. Is there a script I can write that will run everything at once?

The script I have to run:
Li = LineItem.find #####
li.rental_period = ###
li.save!
I have 800 line item numbers to run this for. Your help would be greatly appreciated.
If they all have different rental periods:
line_item_numbers.each do |num|
li = LineItem.find(num)
li.rental_period = custom_rental_period
li.save!
end
If they're all the same, you can just do an update_all:
LineItem.where(id: line_item_numbers).update_all(rental_period: custom_rental_period)
line_item_number_string = line_item_number.join(',')
ActiveRecord::Base.connection.execute("UPDATE line_items SET rental_period=<value> WHERE id IN (#{line_item_number_string})")
while loop
a = 0
b = 4 #this loop will run 4 times
while a < b
Li = LineItem.find #####
li.rental_period = ###
li.save!
a += 1 #adds one to a
end
This is pretty much the same as the first answer, just written in a different way. I find that noobs to ruby can understand this a little easier.
OR
def go
Li = LineItem.find #####
li.rental_period = ###
li.save!
end
then call "go" in your code to execute your syntax
go
thats how you call go

Resources