How to change non scalar variable to scalar variable?

์กฐํšŒ ์ˆ˜: 4 (์ตœ๊ทผ 30์ผ)
Light
Light 2023๋…„ 4์›” 21์ผ
๋Œ“๊ธ€: Walter Roberson 2023๋…„ 5์›” 19์ผ
Good day, im tryin to solve a problem in matlab following steps which were given to us. I am to find the integral of an ode with the lower limit being 0 and the upper limit being x, with x having multiple values. The solution being s or (distance) which will also have multiple values. Im trying to correct the script but I am getting an error because the x limit is not scalar. This is step 3 of 8 and im ultimately trying to complete up to step 8.
This is what I have so far thanks to the help I received:
% k = air resistance coefficient
% ๐œƒ (theta) = initial angle of elevation of the ball when it was kicked
% x and y are respectively the horizontal and vertical spatial coordinates
% t is the time measured from the moment the ball is kicked
% |v| = mag_v = is the magnitude of the velocity (speed, changes with time)
global g
m = 0.45 % in kg
g = 9.81 % gravitational acceleration in m/s^2
v0 = 108 % in km/h
normv = sqrt((v0*cos(theta).^2) + (v0*sin(theta).^2))
t = 0: 0.1: 2;
theta = 30*pi/180; %initial guess for theta
k = 3 %initial guess for air resistance in N
ICs = [0 0 v0*cos(theta) v0*sin(theta)]
% v = 10
% dx/dt = x1
% dx1 / dt = x2
% mx2 = (-k)|v|(x1)
% dy/dt = y1
% dy1 / dt = y2
% my2 = (-mg)(-k)|v|(y1)
% mx2 = @(t,x)[x(1); x(2); -k*mag_v*x(1)]
% my2 = @(t,y)[y(1); y(2); -m*g -k*mag_v*y(2)]
% y(3) = dx/ dt, y(4) = dy/ dt
[T,Y] = ode45(@(t,y)fun(t,y,g,k,m),t,ICs);
%plot(T,Y(:,1:2))
Y(:,1) % X(t)
Y(:,2) % Y(t)
x = [Y(:, 1)]'
y = [Y(:, 2)]'
interp1(Y(:,1), Y(:,2), 0.7)
f = @ (x) sqrt( 1 + (y(4) ./ y(3)).^2 )
quad (f, 0, x)
%s =
function dy = fun(t,y,g,k,m)
mag_v = sqrt(y(3)^2+y(4)^2);
dy = zeros(4,1);
dy(1) = y(3);
dy(2) = y(4);
dy(3) = -k*mag_v*y(3)/m;
dy(4) = (-m*g-k*mag_v*y(4))/m;
end
  ๋Œ“๊ธ€ ์ˆ˜: 12
Harsh
Harsh 2023๋…„ 5์›” 19์ผ
Hi,
Yes, a least squares optimization can be implemented using a while loop.
In the following points, I explain the steps that can be leveraged to achieve the goal.
  • Setting while loop stopping condition: I suggest setting the while loop stop condition according to an error tolerance value instead of stopping the while loop at a zero value. The below provides a sample code for the same:
tol = 0.00001; % Define a tolerance value for the error to stop optimization
while (dE_dtheta > tol || dE_dk > tol)
% Implement the algorithm and variable correction here.
end
  • Implementing the least square optimization algorithm: The algorithm is already specified in the attached PDF document under the โ€œLEAST SQUARES METHODโ€ heading. If you face specific issue in the implementation, feel free to post here.
  • Correcting the variable: The correction in initial guess variables (โ€œkโ€ and โ€œthetaโ€ in this case) with each "while" loop iteration is an important step of optimization. You can use gradient descent in the following way to perform the same:
alpha = 0.001; % Defines the learning rate or step size for gradient descent.
% After calculating error values in current iteration, update the optimization variables as given below.
theta = theta - alpha*dE_dtheta;
k = k - alpha*dE_dk;
Walter Roberson
Walter Roberson 2023๋…„ 5์›” 19์ผ
while V_interpolated >= 0
E = cumsum(V_interpolated - V_var).^2
end
Your while loop must update at least one of the variables that are being tested in the condition, or else you have an infinite loop.
Exception: it is valid to have a loop similar to
while true
do some calculation
if condition is met
break;
end
end
That is, updating the variable or value being tested in a while loop is not strictly necessary if you have a test and a break statement.

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

๋‹ต๋ณ€ (0๊ฐœ)

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Loops and Conditional Statements์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

Community Treasure Hunt

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

Start Hunting!

Translated by