function makehessian(fcn, name) %Creates a new matlab function (defined by gradient(fcn)) %and saves it with the specified name % % Example: makehessian('x^7+x*y^3','hf') creates a file hf.m: % % function functout = hf(v) % x = v(1); % y = v(2); % functout = [[42*x^5, 3*y^2]; [3*y^2, 6*x*y]]; % varlist = symvar(fcn); n=length(varlist); S='';for i=1:n-1, S = [S,char(varlist(i)),',']; end; S = [S,char(varlist(n))]; fid = fopen([name,'.m'],'w'); fprintf(fid,['function functout = ',name,'(v)\n']); for i=1:n fprintf(fid, [char(varlist(i)),' = v(',num2str(i),');\n']); end hessf = char(maple(['hessian(',fcn,',[',S,'])'])); len = length(hessf); newstring = hessf(8:len-1); newstring = strrep(newstring,'], [', ']; ['); fprintf(fid,['functout = ',newstring,';']); fclose(fid);