Error in kinematics (line 22) disp(['Initial velocity: ', num2str(solu), ' m/s'])

2 次查看(过去 30 天)
% Prompt the user for input values
disp('Please enter the following values:')
disp('Enter 0 for the unknown variable')
prompt1 = 'Initial velocity (m/s): ';
u = input(prompt1); % u is the initial velocity
prompt2 = 'Final velocity (m/s): ';
v = input(prompt2); % v is the final velocity
prompt3 = 'Displacement (m): ';
s = input(prompt3); % s is the displacement
prompt4 = 'Acceleration (m/s^2): ';
a = input(prompt4); % a is the acceleration
% Check which variable is unknown
if u == 0 % u is unknown
% Define the symbolic variable
syms u
% Create the equation with the unknown variable
eqn = v^2 == u^2 + 2*a*s;
% Solve the equation for the given values
solu = solve(eqn,u);
% Display the result
disp(['Initial velocity: ', num2str(solu), ' m/s'])
elseif v == 0 % v is unknown
% Define the symbolic variable
syms v
% Create the equation with the unknown variable
eqn = v^2 == u^2 + 2*a*s;
% Solve the equation for the given values
solv = solve(eqn,v);
% Display the result
disp(['Final velocity: ', num2str(solv), ' m/s'])
elseif s == 0 % s is unknown
% Define the symbolic variable
syms s
% Create the equation with the unknown variable
eqn = v^2 == u^2 + 2*a*s;
% Solve the equation for the given values
sols = solve(eqn,s);
% Display the result
disp(['Displacement: ', num2str(sols), ' m'])
elseif a == 0 % a is unknown
% Define the symbolic variable
syms a
% Create the equation with the unknown variable
eqn = v^2 == u^2 + 2*a*s;
% Solve the equation for the given values
sola = solve(eqn,a);
% Display the result
disp(['Acceleration: ', num2str(sola), ' m/s^2'])
else
% Display an error message
disp('Invalid input. Please enter 0 for the unknown variable.')
end

回答(2 个)

Dyuman Joshi
Dyuman Joshi 2024-1-12
编辑:Dyuman Joshi 2024-1-12
The output from solve() will be a symbolic number. And symbolic numbers are not accepted as inputs to num2str, see at the end.
Note the assumption I have included to keep only the real and positive solution.
syms u real positive
solu = solve(29 == u^2 + 2*9.8*1)
solu = 
You can replace the disp() call with fprintf -
fprintf('Initial velocity: %f m/s', solu)
Initial velocity: 3.065942 m/s
%Showing the eror at the end of the post
num2str(solu)
Error using num2str
Input to num2str must be numeric.

Hassaan
Hassaan 2024-1-12
编辑:Hassaan 2024-1-12
% Prompt the user for input values
disp('Please enter the following values:')
disp('Enter 0 for the unknown variable')
prompt1 = 'Initial velocity (m/s): ';
u = input(prompt1); % u is the initial velocity
prompt2 = 'Final velocity (m/s): ';
v = input(prompt2); % v is the final velocity
prompt3 = 'Displacement (m): ';
s = input(prompt3); % s is the displacement
prompt4 = 'Acceleration (m/s^2): ';
a = input(prompt4); % a is the acceleration
% Check which variable is unknown
if u == 0 % u is unknown
% Define the symbolic variable
syms u
% Create the equation with the unknown variable
eqn = v^2 == u^2 + 2*a*s;
% Solve the equation for the given values
solu = solve(eqn, u);
% Display the result
disp(['Initial velocity: ', mat2str(double(solu)), ' m/s'])
elseif v == 0 % v is unknown
% Define the symbolic variable
syms v
% Create the equation with the unknown variable
eqn = v^2 == u^2 + 2*a*s;
% Solve the equation for the given values
solv = solve(eqn, v);
% Display the result
disp(['Final velocity: ', num2str(double(solv)), ' m/s'])
elseif s == 0 % s is unknown
% Define the symbolic variable
syms s
% Create the equation with the unknown variable
eqn = v^2 == u^2 + 2*a*s;
% Solve the equation for the given values
sols = solve(eqn, s);
% Display the result
disp(['Displacement: ', num2str(double(sols)), ' m'])
elseif a == 0 % a is unknown
% Define the symbolic variable
syms a
% Create the equation with the unknown variable
eqn = v^2 == u^2 + 2*a*s;
% Solve the equation for the given values
sola = solve(eqn, a);
% Display the result
disp(['Acceleration: ', num2str(double(sola)), ' m/s^2'])
else
% Display an error message if no variable is set to 0
disp('Invalid input. Please enter 0 for the unknown variable.')
end
I replaced num2str with mat2str, which converts the entire array of solutions to a string format that can be concatenated with other strings. If you still get multiple solutions and you need to choose the physically meaningful one, you might need to add a check to filter out the solutions based on your specific requirements (like selecting only positive solutions).
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by