Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
function f = objfun(x)
f = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);
x0=[-1,1];
options = optimoptions(#fminunc,'Algorithm','quasi-newton');
[x,fval,exitflag,output] = fminunc(#objfun,x0,options);
x,fval,exitflag,output
end
Can you please help me in running the code?
Convert f to a function handle as
fun = #(x) exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);
then call fminunc with
[x,fval,exitflag,output] = fminunc(fun,x0,options);
As a side note, don't ever call fminunc from within the objective function.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
Hi i am trying to get two returns from the following code in MATLAB:
function [Xq, SNq] = cuantificacion(x,xmax,xmin,b)
N = input('Introduce un numero de muestras: ');
L = 2^b;
delta = (xmax-xmin)/L;
if(abs(x)<xmax)
Xq = (fix((abs(x)/delta)) + 1/2)*delta*sign(x);
else
Xq = ((L-1)/2)*delta*sign(x);
end
p = 0;
q = 0;
for i = 0:N
p = p+x^2;
q = q + (Xq - x);
end
Px = 1/N*p;
Pq = 1/N*q;
SNq = 10*log(Px/Pq);
end
But i only get one return and I don't understand why.
Thanks to Ander i was just calling it wrong. I had to call it:
[return1, return2] = cuantificacion(x,xmax,xmin,b);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have this trigonometric equation;
cos(2*pi*50*t)+cos(2*pi*100*t)
I want to graphic of equation and I want to find field for a period. How can I do?
Graph:
>> f = #(t) cos(2*pi*50*t) + cos(2*pi*100*t);
>> x = linspace(0, 1/50, 100);
>> y = f(x);
>> plot(x,y)
Area over 1 period:
>> integral(f, 0, 1/50)
or just do it manually:
∫ ( cos(2π·50t) + cos(2π·100t) ) dt =
-1/2π·( 1/50·sin(2π·50t) + 1/100·sin(2π·100t) )
which, evaluated between 0 and 1/50, equals 0.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a function (the hill function) when X and K are the same (x==k) the output should be 0.5, when I test the function it gives a result of 0.5, when I try to plot it, I do not get 0.5 for my Y. Can anyone explain what I am doing wrong?
n = 1;
k = 1;
x = [0:0.01:2].';
y = (x.^n)/((x.^n)+(k.^n));
plot(x,y);
n = 1;
k = 1;
x = [0:0.01:2].';
y = (x.^n)./((x.^n)+(k.^n));
plot(x,y);
You missed a dot before the division.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
Let's say I have this function
f(t) = 4*sin(a(t)) + x(t)*y(t) + h + cos(y(t))*sin(x(t))
How would I compute its derivative with respect to time?
You need to declare the variables and the functions inside it as being symbolic and then use diff:
clear
clc
syms a x y t h
a(t) = symfun(sym('a(t)'), t)
x(t) = symfun(sym('x(t)'), t)
y(t) = symfun(sym('y(t)'), t)
F = 4*sin(a(t)) + x(t)*y(t) + h + cos(y(t))*sin(x(t))
DerF_t = diff(F,t)
Giving the following (messy) output:
F = h + 4*sin(a(t)) + cos(y(t))*sin(x(t)) + x(t)*y(t)
DerF_t = x(t)*diff(y(t), t) + y(t)*diff(x(t), t) + 4*cos(a(t))*diff(a(t), t) + cos(x(t))*cos(y(t))*diff(x(t), t) - sin(x(t))*sin(y(t))*diff(y(t), t)
Note that since a(t),x(t) and y(t) are simply defined as functions of 't' we are stuck with their 'symbolic' derivative (I don't know the term for that sorry)...i.e. diff(a(t)) for instance.
Hope it's what you were after!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Given the following code:
t = -5:.1:5;
w = pi;
x = zeros(101,1);
Xt = zeros(101,1);
for i = 1 : 101;
x = exp((-3*t)+(-1i*w*t));
Xt = trapz(t, x);
end
disp (length(x))
disp (length(Xt))
disp (Xt)
The values of Xt do not change, which is a problem.
How should this be coded in order for Xt to change when x is changed?
Side note:
Xt(i) = trapz(t,x);
Reduces the vector from length 101 to length 1 and therefore cannot be used.
i am not sure if this is what you wanted. anyways while working on imaginary numbers it is always a good idea to not use i and j as common variables, just to avoid confusion (IMO)
xt = zeros(101,1);
x = exp((-3.*t)+(-1i*w.*t));
for k=2:101
xt(k)=trapz(t(1:k),x(1:k));
end