Question about calling user defined function to another script
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hello
first here is my user_defined_function code.(name is A_execute1.m)
function A=A_execute1(r)
global V;
A=pi*r*(sqrt(r^2+((9*V^2)/(pi^2*r^4))));
end
second, this is the script file(i made) that i want to call A_execute1 file to.
(to draw graph to see)
r=[-100:0.01:100];
V=10;
fminbnd('A_execute1',-100, 100)
plot(r,A_execute1(r)), xlabel('r'), ylabel('A')
grid
but it says error that
Error using fminbnd (line 237)
User supplied objective function must return a scalar
value.
Error in test4 (line 3)
fminbnd('A_execute1',-100, 100)
why it says that error?
i think i designed scalar value, not vector.
1 commentaire
Stephen23
le 27 Oct 2022
Do not use global variablesto pass parameter values, use the methods shown here: https://www.mathworks.com/help/matlab/math/parameterizing-functions.html
Réponse acceptée
Matt J
le 27 Oct 2022
Modifié(e) : Matt J
le 27 Oct 2022
It returns empty.
%r=[-100:0.01:100];
V=10;
A_execute1(-100)
[r,fval]=fminbnd(@(r) A_execute2(r,V),-100,100)
function A=A_execute1(r)
global V;
A=pi*r*(sqrt(r^2+((9*V^2)/(pi^2*r^4))));
end
function A=A_execute2(r,V)
A=pi*r*(sqrt(r^2+((9*V^2)/(pi^2*r^4))));
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!