Automating Changing Partial Derivatives
Afficher commentaires plus anciens
I'm trying to create a program to automate Kline-McClintock Uncertainty analysis'.
My only problem with my current script is in the %% Declaring Variables section. I don't know how I can define a variable, which was symbolic, to equal a number so that I can evaluate the partials. I want to do this in a way that I can put it into a for loop and it will work for any number of variables. My only thought is to edit the script by scanf within itself.
clear all; close all; clc;
%%Declare Stuff
Variables = 'M t L h1 h2 g D rho';
Eq = '((h1-h2)*2*g*D*rho^2*t^2*(pi/4*D^2)^2)/(L*M^2)';
Conversion = 1;
string.Units = '';
string.Eq = ' for f';
% Example = Var Value Var Error % Var Unit
%------------------------------------------------------------------
Variable_Values = [2.95 0.005 ; % M Kg
30.07 0.1 ; % t sec
360E-3 1E-3 ; % L m
29.5E-2 0.5E-2 ; % h1 m
11E-2 0.5E-2 ; % h2 m
9.81 0.002 ; % g m/s^2
8.6E-3 0.3E-3 ; % D m
999 1 ]; % rho kg/m^3
%%Gogo Matlab
index = 0;
string.old = [];
for i = 1:length(Variables)
if ~isspace(Variables(i)) && i ~= length(Variables)
string.new = sprintf('%c',Variables(i));
string.old = strcat(string.old,string.new);
elseif i == length(Variables)
index = index + 1;
string.new = sprintf('%c',Variables(i));
string.old = strcat(string.old,string.new);
Symbolic_Variables(index) = sym(string.old);
string.old = [];
else
index = index + 1;
Symbolic_Variables(index) = sym(string.old);
string.old = [];
end
end
%%Finding Partials
% Display Initial Equation
string.Initial_Eq = sprintf('Initial Equation%s:\n%s\n',string.Eq,char(Eq));
disp(string.Initial_Eq)
% Calculate and Display Partials
for i = 1:length(Symbolic_Variables)
string.Partial = sprintf('Partial wrt %s:', ...
char(Symbolic_Variables(i)));
Partial(i) = diff(Eq,Symbolic_Variables(i));
if Partial(i) ~= 0
disp(string.Partial)
disp(Partial(i))
end
end
%%Declaring Variables
M = Variable_Values(1,1);
t = Variable_Values(2,1);
L = Variable_Values(3,1);
h1 = Variable_Values(4,1);
h2 = Variable_Values(5,1);
g = Variable_Values(6,1);
D = Variable_Values(7,1);
rho = Variable_Values(8,1);
%%Computing Uncertainty
uncertainty = 0;
for i = 1:length(Symbolic_Variables)
uncertainty = (eval(Partial(i))*Variable_Values(i,2))^2 + uncertainty;
end
uncertainty = sqrt(uncertainty)*Conversion;
%%Display Answer
string.Answer = sprintf('The uncertainty is %.4f%s%s',uncertainty,...
string.Units,string.Eq);
disp(string.Answer)
Réponse acceptée
Plus de réponses (1)
Travis
le 13 Avr 2011
0 votes
Catégories
En savoir plus sur Number Theory dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!