if x is a variable, solve for x

조회 수: 2 (최근 30일)
Kyle Langford
Kyle Langford 2021년 4월 8일
편집: Adam Danz 2021년 4월 8일
I am open to any better ways of doing this. I am brand new to Matlab.
What I am trying to do is write a code to solve kinematic equations, and if the user inputs one of the prompts as a variable, then display the correlating variable. Example:
When promted
what is your x value? x %user inputs a variable as itself
what is your x_0 value? 0
what is your v_0 value? 5
what is your t value? 10
Final distance = 50
for any of the given options.
Hopefully this makes sense.
syms x x_0 v_0 t
equation = x==x_0+v_0*t
x= input('What is your x value?');
x_0 = input('What is your x_0 value?');
v_0 = input('What is your v_0 value?');
t = input('What is your t value?');
equation = x==x_0+v_0*t;
S=solve(equation,"IgnoreProperties",true);
if x==x
fprintf('Distance is %d\n', S)
else
;
end
if x_0==x_0
fprintf('Initial Distance is %d\n', S)
else
;
end
if v_0==v_0
fprintf('Initial Velocity is %d\n', S)
else
;
end
if t==t
fprintf('Time is %d\n', S)
else
;
end

채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 8일
If the user inputs a variable name for exactly one of the prompts, then symvar(equation) will tell you which variable it was.
solving_for = symvar(equation);
switch solving_for
case sym('x')
name = 'Distance';
case sym('x_0')
name = 'Initial Distance';
case sym('v_0')
name = 'Initial Velocity';
case sym('t')
name = 'Time';
otherwise
error('wrong variable to solve for');
end
fprintf('%s is %g\n', name, S);
  댓글 수: 1
Kyle Langford
Kyle Langford 2021년 4월 8일
Awesome! Thank you! We have not covered any of that in my class yet.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by