Info

This question is closed. Reopen it to edit or answer.

implementing a function that contains an integral

2 views (last 30 days)
eya
eya on 3 Jun 2011
Closed: MATLAB Answer Bot on 20 Aug 2021
good morning, i have a litle problem here, wich is: i need to make a code that assure the optimization of a fonction, by finding the optimums parameters that approach the experimental values to the theoretical function , the problem is that the téorical function contains an integral, and i couldn't write it correctly, so it shows a wrong ansers, here is the code, please hepl me to find where i'm i wrong
function main
global ti yi
ti = [3 12 24 48 168];
yi = [0.6 0.7 1 1.2 1.4];
a0 = [10 0.4];
format long
asol = fminsearch ( @F, a0)
plot (ti, yi, '*', ti, f(asol, ti))
function y = f(a,t)
myfun = inline('v.*(((a(1)-1).*(a(2)*1400*t))+((-16.7.*v).^(1-a(1)))).^(1/(1-a(1)))','a','t','v');
for k = 1:length(t)
fc(k) =quad(@(v)myfun(a,t(k),v), -1.8, 1.8);
end
y=2.9-0.035.*fc;
function out = F(a)
global ti yi
out = sum ( (yi - f(a,ti)).^2 );

Answers (2)

Mike Hosea
Mike Hosea on 3 Jun 2011
Well, as an aside, you shouldn't need to use 'inline', rather
myfun = @(a,t,v)(blah, blah, blah)
Secondly, I don't know what is going on, but I'm wondering whether you expected to generate complex numbers. Maybe there's an error in myfun. -- Mike

eya
eya on 3 Jun 2011
no i'm not expecting to generate complex numbers, but rather real numbers, in fact i need the value of the vector asol, wich contains a(1) and a(2), and wich is initialised by a0, but whatever i put as initialisation of a0, the output asol is always equal to a0 wich is wrong ang ilogical, and i don't get what going on wrong
  1 Comment
Mike Hosea
Mike Hosea on 4 Jun 2011
My point is that if you integrate that function as specified, it gives you complex results. Suppose a(1) = 10.5, then the term (-16.7*v)^(1-a(1)) = (-16.7*v)^(-9.5). This term has a singularity at v=0, and furthermore, the term is calculates as exp(-9.5*log(-16.7*v)), so for positive v this involves log of a negative number.

This question is closed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!