Effacer les filtres
Effacer les filtres

How to implement a function within another one ?

3 vues (au cours des 30 derniers jours)
Martin Matin
Martin Matin le 18 Mar 2015
Commenté : Martin Matin le 20 Mar 2015
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]
%end of datas
for m = 1:5
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 :');
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
vmax = 150;
fvmin(m) = f(vmin(m));
fvmax = f(vmax);
if fvmax*fvmin(m) > 0
disp('Valeurs de même signe.');
root = (vmax + vmin(m))/2;
err = abs(f(root));
while err > 1e-7
if fvmax*f(root)<0
vmin(m) = root;
vmax = root;
end %end of if
root = (vmax + vmin(m))/2;
err = abs(f(root));
end %end of while loop
end %end of for loop

Réponse acceptée

John le 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 commentaires
John le 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 le 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Xia le 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 commentaire
Martin Matin
Martin Matin le 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".

Connectez-vous pour commenter.


En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by