# Why Is My MATLAB Simulation of Orbiting Masses Not Running?

• eltrinco
In summary, Matlab is a high-level programming language and interactive environment commonly used in scientific and engineering applications. It allows for efficient data analysis, visualization, and computation. A Matlab problem refers to a specific issue or challenge encountered while using the Matlab software, such as errors in code, incorrect results, or difficulties with data processing. The best way to solve a Matlab problem is to break it down into smaller, manageable steps and use the tools and functions available in Matlab to troubleshoot and debug the issue. Motion objects in Matlab refer to graphical representations of an object's movement, typically created using the <code>plot</code> function. To plot a motion object in Matlab, one can use the <code>plot</code> function and provide
eltrinco

## Homework Statement

I have created random mass and try to make them go around each other . somehow i couldn't find where all the code goes wrong. how do i fix this

## The Attempt at a Solution

I have coded this. but still the file can't be run. i don't know where to fix this .

% Creating initial positions/velocities (Randomly)
clear all
close all
clc
n_masses=10;
m=rand(1,n_masses) %RANDOM masses fore each body IF YOU WANT
% the same mass for all use instead :
%m=k*ones(1,n_masses) where k is the mass

x=randn(1,n_masses); %X position
y=randn(1,n_masses); %Y position
vx(1,:)=randn(1,n_masses); %X component velocity (initial)
vy(1,:)=randn(1,n_masses) %Y component velocity (initial)
scatter(x,y,round(100.*m),'filled','blue') % different masses, different sizes
title('Initial configuration')
figure

% Calculating force matrix (F{i,j} means Force exterted by the i-th
% particle on the j-th.
%when j=i -> force is zero
[F,F_totalx,F_totaly]=calculating_F_matrix(x,y,m);

%Evolution
dt=0.05; %interval
n_steps=40; %number of timesteps
%starting positions for the simulation
xx{1}=x; %Will contain the succession of the X positions
yy{1}=y; %Will contain the succession of the Y positions

for s=1:n_steps+1
disp(['Simulating at time t=' num2str(dt*(s-1))]);
for i=1:n_masses
[F,F_totalx,F_totaly]=calculating_F_matrix(xx{s},yy{s},m);
%initial accelerations (fixed)
ax(s,:)=F_totalx;
ay(s,:)=F_totaly;

%Updating velocities
vx(s+1,i)=vx(s,i)+dt*ax(s,i);
vy(s+1,i)=vy(s,i)+dt*ay(s,i);

%Updating positions
xx{s+1}(i)=xx{s}(i)+dt*xx{s}(i);
yy{s+1}(i)=yy{s}(i)+dt*yy{s}(i);

end
end

% Graphics (plot the evolution of the particles)
%find max x and y to correctly set the reference for the plot
for i=1:length(xx)
xm(i)=max((abs(xx{i})));
ym(i)=max(abs(yy{i}) );
end

axis (1.1.*[-max(xm) max(xm) -max(ym) max(ym) ]); % set axis to see all particles
for s=1:n_steps
scatter(xx{s},yy{s},round(100.*m),'filled','red')
axis (1.2.*[-max(xm) max(xm) -max(ym) max(ym) ]); % set axis to see all particles
title(['Time t=' num2str(dt*(s-1))])
pause(0.15)
end

function [F,F_totalx,F_totaly]=calculating_F_matrix(2,3,5)
n_masses=length(m);
G=6.67*10^(-11); % Universal gravity constant
for i=1:n_masses %influenced index
for j=1:n_masses %index generating field
if j~=i % excluding j=i
Xdistance=abs(x(i)-x(j));
Ydistance=abs(y(i)-y(j));
%distance=sqrt((x(i)-x(j)).^2 )%+(y(i)-y(j)).^2); %distance i-j
% dij=(x(i)-x(j)) %unitary vector dij
F{i,j}(1)=G*m(i)*m(j)/(Xdistance);
F{i,j}(2)=G*m(i)*m(j)/(Ydistance);
else
F{i,j}(1)=0;
F{i,j}(2)=0;
end
end
end

% Calculating the total force (per unit of mass m_i) acting on i-th particle (by components)
F_totalx(1)=0;
F_totaly(1)=0;
for i=1:n_masses
Z=cell2mat(F(i,:)'); %vector of all forces acting on the i-th particle
F_totalx(i)=sum(Z(:,1))./m(i);
F_totaly(i)=sum(Z(:,2))./m(i);
end

end

Hello,

Thank you for reaching out for help with your code. It looks like you have made a good attempt at creating random masses and making them go around each other. However, there are a few areas where you can improve your code to make it run successfully.

1. Make sure to define all variables: In your code, you have defined n_masses, m, x, y, vx, vy, F, F_totalx, F_totaly, dt, and n_steps. However, you have not defined ax, ay, xx, yy, or xm and ym. Make sure to define these variables before using them in your code.

2. Check for errors: When you are running your code, make sure to check for any error messages that appear in the command window. These messages can give you helpful information about where your code is going wrong.

3. Check for syntax errors: Make sure to use proper syntax when defining and using functions in your code. For example, in your function calculating_F_matrix, you have defined the input variables as 2, 3, and 5 instead of using the variables x, y, and m that you defined earlier in your code. Also, make sure to use proper indentation and spacing to make your code easier to read and debug.

## 1. What is Matlab?

Matlab is a high-level programming language and interactive environment commonly used in scientific and engineering applications. It allows for efficient data analysis, visualization, and computation.

## 2. What is a Matlab problem?

A Matlab problem refers to a specific issue or challenge encountered while using the Matlab software, such as errors in code, incorrect results, or difficulties with data processing.

## 3. How can I solve a Matlab problem?

The best way to solve a Matlab problem is to break it down into smaller, manageable steps and use the tools and functions available in Matlab to troubleshoot and debug the issue. It can also be helpful to consult online resources or seek assistance from experienced Matlab users.

## 4. What is motion object in Matlab?

A motion object in Matlab refers to a graphical representation of an object's movement, typically created using the plot function. It can be used to analyze and visualize the motion of objects in various scenarios, such as in physics or engineering simulations.

## 5. How can I plot a motion object in Matlab?

To plot a motion object in Matlab, you can use the plot function and provide the necessary parameters, such as the object's position, velocity, and time. You can also customize the plot using various formatting options and add labels and annotations to make the visualization more informative.

• Engineering and Comp Sci Homework Help
Replies
3
Views
1K
• Biology and Medical
Replies
1
Views
1K
• Calculus and Beyond Homework Help
Replies
2
Views
419
• Engineering and Comp Sci Homework Help
Replies
3
Views
2K
• Engineering and Comp Sci Homework Help
Replies
1
Views
2K
• Engineering and Comp Sci Homework Help
Replies
5
Views
2K
• Programming and Computer Science
Replies
9
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
• Engineering and Comp Sci Homework Help
Replies
5
Views
1K
• Programming and Computer Science
Replies
1
Views
2K