Solve() in a for loop giving an Empty sym: 1-by-0 answer

I've been working on a code to calculate velocity and acceleration. The issue is with the last section called %%Solve for position at time at a=0 and v=0 and the third function. I'm trying to get an array of positions at the times listed. I keep getting Empty sym: 1-by-0 as the answer.
I'll copy the function codes I've written as well as the document combining the functions. Let me know if you can help.
First functions:
function [velocity,acceleration] = Velocity_Acceleration( position )
%Velocity_Acceleration calculates the first and second differencial of
%position
% this takes a given equation and turns it into a diff equation
velocity = diff( position ); % this is the velocity of a given equation
acceleration = diff( position, 2);% this it the acceleration of a given equation
end
Second functions:
function [time] = SolveforTime(equation, y, t1)
%SolveforTime solves for a unknow (t) variable if the answer for the
%equation is known
% tvalue is the unknown solution in the equation
% equation is the given equation
% whattheequationisequalto is the number the equation is equal to
% t1 is the name of the constant to be identified aka tvalue
whattheequationisequalto = y==equation;
time = solve(whattheequationisequalto, t1);
end
Third functions:
function [time] = SolveforXifTimeisKnown(eqn,t,t1)
%SolveforTime solves for a unknow (x) variable if the time for the
%equation is known
% tvalue is the unknown solution in the equation
% equation is the given equation
% whattheequationisequalto is the number the equation is equal to
% t1 is the name of the constant to be identified aka tvalue
counter = 1;
t1 = t1;
% first for loop runs for the primes of 1000
% ai is the individual number (1000) being checked in the loop
for i = length(t1)
ti = t1(i);
eqns = [eqn, t==ti];
t2 = solve(eqns,t);
time(counter,: ) = [t2];
counter = 1 + counter;
end
time
end
Main code:
% Determine (a) when the velocity is zero, (b) the position and total distance traveled when the acceleration is zero.
%
% If the motion of a particle is defined by the relation x = t^3 - 9*t^2 +
% 24*t - 8, where x and t are expressed in inches and seconds, respectively.
%
% use this to take a simple position equation in terms of time to find
% velocity and acceleration
clear
clc
%% what is the given position equation and what velocity and acceleration
% needing to find
% t represents time
syms t;
% insert any equation in terms of t to get the first (v) and second(a)
% differential equation of x
x = t^3 - 9*t^2 + 24*t - 8;
v1 = 0;
a1 = 0;
x0 = 0;
%% solve for Velocity and Acceleration equations
%
fprintf('If position is: %s \nThen\n\n', x);
% this calls the differential function
[v,a] = Velocity_Acceleration(x);
fprintf('velocity is: ');
disp(v);
fprintf('acceleration is: ');
disp(a);
%% part a
%
equation = v; % choose v or a for which one you want to find.
v3 = v1; % this is what v or a equation above equals :
% this solves for t if velocity, acceleration, or position is known
tvalue1 = SolveforTime(equation, v3, t)';
a2 = tvalue1(:,1);
b2 = tvalue1(:,2);
% I'm just displaying what part of the problem is, this is part a
disp('a)');
% this shows the numbers solved for. for this one it's time
fprintf(' If the velocity is 0, time = %g and %g.\n\n\n', a2, b2);
%% part b
%
equation = a; % choose v or a for which one you want to find.
y2 = a1; % this is what v or a equation above equals :
% this solves for t if velocity, acceleration, or position is known
tvalue2 = SolveforTime(equation, y2, t);
% I'm just displaying what part of the problem is, this is part a
disp('b)')
% this shows the numbers solved for. for this one it's time
fprintf(' If the accileration is 0, time = %g.\n\n', tvalue2)
%% Solve for position at time at a=0 and v=0
%
t1 =[a2, tvalue2, b2];
t2 = length(t1);
syms x5 [1 t2];
x1 = x5 == x;
x2 = SolveforXifTimeisKnown(x1,t,t1)';

Answers (1)

x = t^3 - 9*t^2 + 24*t - 8;
x will be a single scalar symbolic expression
t1 =[a2, tvalue2, b2];
t1 will be at least 3 values
t2 = length(t1);
syms x5 [1 t2];
x1 = x5 == x;
so x1 will be a vector with one equation for each t1 value.
for i = length(t1)
ti = t1(i);
eqns = [eqn, t==ti];
t2 = solve(eqns,t);
ti will be a specific value for t. In particular, it will be the last t1 value, because you used i = length(t1) rather than i = 1:length(t1)
t==ti will effectively constrain t to be a single specific value for the purpose of the equations.
Then you solve the augmented system of three equations of the form XVARIABLE = t^3 - 9*t^2 + 24*t - 8 with the same t^3 - 9*t^2 + 24*t - 8 being used for each of the cases, but different XVARIABLE for the three equations. And you add on the t == specific value. Then you ask to solve that array of four equations for t . There is of course, only one solution, which is that t must be the ti value, since you have the equation t==ti .
Remember that the name of the function indicates that you are solving for x not for t
There is a much much easier way to proceed if you want to solve for x for a vector of t1 values:
x = subs(eqn, t, t1);
with no solve needed.

Asked:

on 5 Mar 2023

Answered:

on 5 Mar 2023

Community Treasure Hunt

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

Start Hunting!