# Trying to calculate values given an array of x, y, and stiffness values.

4 views (last 30 days)
Keaton Looper on 21 Feb 2024
Edited: Rupesh on 21 Mar 2024
I have arrays of x, y, and stiffness values and i am trying to calculate potential energy from each part due to spring stiffness and weight.
Below is some of the code
S = 5;
M = 6;
Z = 7
Y = 7
W=zeros(S,1);
K=zeros(M,1);
x=zeros(Z,1)
y=zeros(Y,1)
% Constants
L = 10 * ones(1,6); % initial spring length
% Computation of potential energy
for i = 1:M
K(i,1) = 30 + 40*(9/2-i).^2 % stiffness for spring i = 1-6
j = 1:S;
W(j,1) = 40-7*j
end
for k = 1:Z
x(k,1)=10*(k-1);
y(Y,1)=0
end
g = 9.81
##### 3 CommentsShow 1 older commentHide 1 older comment
Keaton Looper on 22 Feb 2024
Define the potential energy in MATLAB as a function of the coordinates {𝑥 , 𝑦}& for all nodes. Use the function fminunc in MATLAB to find the equilibrium position and report the coordinates {𝑥 , 𝑦}& for all nodes at equilibrium. Note that the coordinates {𝑥 , 𝑦} of the nodes at the ends are not variables.
Dyuman Joshi on 22 Feb 2024
That's the question you were given.
What is your question to the forum?
You have not implemented the fminunc above. Refer to the documentation for reference.

Sign in to comment.

### Answers (1)

Rupesh on 19 Mar 2024
Edited: Rupesh on 21 Mar 2024
Hi Keaton,
I understand that you are trying to find the equilibrium positions of nodes connected by springs of varying stiffness, under the influence of gravity. To achieve this, you'll calculate the system's total potential energy, which includes both the energy stored in the springs and the gravitational potential energy of the nodes. By minimizing this total potential energy, you can determine the positions at which the system is in equilibrium. Here's a step-by-step approach, combined with pseudo code, to guide you through the process:
1. Define PotentialEnergyFunction(nodes_positions):
• Calculate spring_energy for each spring based on its stiffness, the natural length, and the current length between connected nodes.
• Calculate gravitational energy for each node based on its weight and vertical position.
• total_potential_energy = sum of all spring_energy and gravitational_energy.
• Return total_potential_energy.
function totalPE = PotentialEnergyFunction(nodePositions, K, W, L, g)
spring_energy = 0;
gravitational_energy = 0;
% Assuming nodePositions contains [x1, y1, x2, y2, ...]
% Calculate spring energy for each spring
for i = 1:length(K)
deltaX = nodePositions(2*i-1) - nodePositions(2*i+1);
deltaY = nodePositions(2*i) - nodePositions(2*i+2);
springLength = sqrt(deltaX^2 + deltaY^2);
spring_energy = spring_energy + 0.5 * K(i) * (springLength - L(i))^2;
end
% Calculate gravitational energy for each node
for j = 1:length(W)
gravitational_energy = gravitational_energy + W(j) * g * nodePositions(2*j);
end
totalPE = spring_energy + gravitational_energy;
end
2. Initialize nodes_positions with an initial guess.
• This could be a linear distribution or any configuration you deem close to equilibrium.
% Assuming x and y are already defined as per your initial code
initialPositions = [x; y]; % Combine x and y for initial guess
3. Use “fminunc” to find equilibrium positions:
• Call fminunc(PotentialEnergyFunction, nodes_positions), which optimizes "nodes_positions" to minimize the "total potential energy".
% Optimization using fminunc
options = optimoptions('fminunc', 'Algorithm', 'quasi-newton');
[nodePositions_optimized, fval] = fminunc(@(nodePositions) PotentialEnergyFunction(nodePositions, K, W, L, g), initialPositions, options);
4. Output the optimized nodes_positions as the equilibrium state.
• These positions represent the state where the system's total potential energy is at its minimum, indicating equilibrium.
This approach utilizes the MATLAB’s “fminunc” function and leverages the power of optimization to find the equilibrium state efficiently. By accurately defining the potential energy function and providing a reasonable initial guess, you set the stage for “fminunc” to iteratively adjust the nodes' positions until it finds the configuration where the system's total potential energy is minimized. This configuration represents the equilibrium positions of the nodes, considering the complex interplay between spring forces and gravitational forces.
Note: This example assumes that the spring's rest length (L0) is the same for all springs, and it linearly distributes the initial positions of the nodes along the x-axis. You might need to adjust the initial conditions or other parameters based on your specific problem setup.
You can also refer to the below documents regarding operations of the fminunc functions involved in the above script.
Hope this helps!
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Assembly in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!