trouble with code to form robust solver that can handle different unknowns
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
aldburg
le 20 Nov 2017
Réponse apportée : Walter Roberson
le 20 Nov 2017
I am trying to write some code that can identify an unknown and solve an equation with respect to it. For example, my input box asks for values for x,y,z, and w. At the moment if I enter values for everything except x it will find a solution for x and prompt the user, but it only works for x. How can I modify my code so it can solve for another variable without explicitly telling it what unknown it will be?
clear
prompt = {'x','y','z','w'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'...','...','...','...'};
options.Interpreter = 'tex';
answer = inputdlg(prompt,dlg_title,num_lines,defaultans,options)
test_for_numbers=str2double(answer);
if isnan(test_for_numbers)
% str is a "symbol"
else
% num is numeric value
end
syms x
Solution = solve(x+test_for_numbers(2,1)+test_for_numbers(3,1)+test_for_numbers(4,1)== 100, x)
x = Solution
CreateStruct.Interpreter = 'tex';
CreateStruct.WindowStyle = 'modal';
h=msgbox(sprintf('x = %2.3g',x),'x',CreateStruct);
0 commentaires
Réponse acceptée
Walter Roberson
le 20 Nov 2017
You can use symvar() on a character string (even without the Symbolic toolbox.)
0 commentaires
Plus de réponses (0)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!