How to implement a function within another one ?

1 view (last 30 days)
Hi, I'd like to use the bisection method to find the root of a function "Fetoile" The method works but I have to iterate it for some values of a parameter "i". The problem is, the bisection function does not use "i".
Here is the code that gives me the results for "Fetoile" :
function Fetoile = q4(v)
% datas
vitesse_rotation= [83.7758040957278, 119.587013890941, 155.398223686155, 191.209433481369, 227.020643276582, 262.831853071796, 298.643062867009, 334.454272662223, 370.265482457437, 406.07669225265, 441.887902047864, 477.699111843078, 513.510321638291, 549.321531433505, 585.132741228718, 620.943951023932, 656.755160819146, 692.566370614359, 728.377580409573, 764.188790204786]
couple=[206.41064280052, 243.796265372056, 256.741969803922, 258.849308707433, 255.11836152428, 247.801181783381, 238.039957653591, 226.445249315901, 213.320493034029, 198.699623003111, 181.889713594511, 163.718280099678, 145.175662563345, 126.558747418095, 108.009552378088, 89.6025609484197, 71.3773301900352, 53.3538548851947, 35.5406860579882, 17.9395273160688]
rho=1.2;
S=2.28;
Cx=0.31;
mveh=1360;
g=9.81;
f0=0.0136;
f2=4.*10.^(-7);
ntrans=0.92;
atrans=1.04;
btrans=0.0025;
R=0.31;
i=[9.62,5.87,3.91,2.90,2.31];
teta=0;
%end of datas
for m = 1:5
Cmax=spline(vitesse_rotation,couple,i(m).*(v)/R);
Faero = (1/2)*rho*S*Cx*(v).^2;
Frlt = mveh*g*cos(teta)*(f0+f2*(v).^2);
Fgrav = mveh*g*sin(teta);
Fmotmax = ntrans*(i(m)/R)*Cmax;
Fetoile = Faero + Frlt + Fgrav - Fmotmax;
tabFetoile(m) = Fetoile;
end %end of for loop
disp('The results are :');
disp(tabFetoile);
And here is the code for the bisection, I tried a loop on the values of "vmin" are values that I'm sure are negatives.
function root = bishoz(f,vmin,vmax)
resultat = [];
for m = 1:5
vmin=[6,9.5,13.5,16.5,19];
vmax = 150;
fvmin(m) = f(vmin(m));
fvmax = f(vmax);
if fvmax*fvmin(m) > 0
disp('Valeurs de même signe.');
else
root = (vmax + vmin(m))/2;
err = abs(f(root));
end
while err > 1e-7
if fvmax*f(root)<0
vmin(m) = root;
else
vmax = root;
end %end of if
root = (vmax + vmin(m))/2;
err = abs(f(root));
end %end of while loop
resultat(m)=root;
end %end of for loop
disp(resultat);

Accepted Answer

John
John on 18 Mar 2015
I read through your example and it looks like the language functionality you are looking for are nested functions. Please see this for more info http://www.mathworks.com/help/matlab/matlab_prog/nested-functions.html
  3 Comments
John
John on 19 Mar 2015
I do not understand what you are looking for Martin. Are you having problems calling the nested function from the outer main function?
It would help if you could provide a simple code example. That would help greatly. I have a feeling the answer you are looking for is close to hand. So please help us with a small example.
Martin Matin
Martin Matin on 20 Mar 2015
It is okay, I finally did by making a script which I looped 5 times and I used a function handle to call q4.

Sign in to comment.

More Answers (1)

Xia
Xia on 18 Mar 2015
Sorry I read your code twice but got lost. Would you please epitomize your question by a simple example? Maybe it'll be a little better for other guys to get into this question..
  1 Comment
Martin Matin
Martin Matin on 18 Mar 2015
I have a function that depends of "i" and "v" I have another function, bisecting the first one.
I want to make to make the bisection vary in terms of "i". But because the reach of "i" is only the first one, it doesn't work. It gives me 5 times the last value of "i".

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!