I want to write a function that takes the output of one function and puts it into another. I am using the potential energy calculation of one equation and putting it into the kinetic energy of another but I am getting errors. I also need to plot it.

8 vues (au cours des 30 derniers jours)
function [Penergy] = energy(start, inc, stop)
% takes a range of input values
for potential = start:inc:stop
% calculates the potentail energy of a spring
Penergy = ((1/2) * 1000 * potential^2);
disp(Penergy);
end
% plots the output data to a graph
fplot ('(1/2) * 1000 * potential^2',[.005,.025])
end
function [Kenergy] = Kenergy(start, inc, stop)
% calculates velocity
v = sqrt((Penergy * 2)/.01);
for potential = start:inc:stop
Kenergy = (1/2) * .01 * v^2;
disp(Kenergy);
end
fplot ('(1/2) * .01 * v^2', [.005, .025]);
end
  6 commentaires
Rik
Rik le 6 Juin 2018
If the Kenergy function has an m-file of its own, then there are two options:
  1. it results in an error, as Penergy is not an internal Matlab function, nor is it declared as a variable before it is used
  2. Penergy is a function itself
The error you got doesn't match the first situation, so either you have an m-file with the name Penergy, or this function is not in its own file. There is something causing a loop, so my guess would be that Penergy is a function that calls Kenergy.
Stephen23
Stephen23 le 6 Juin 2018
Modifié(e) : Stephen23 le 6 Juin 2018
@Zachary Giovanelli: Penergy is not defined as an input to the function Kenergy, nor does Kenergy appear to be nested inside another function. Therefore it is not clear how Penergy is ever defined with a value inside the function Kenergy.

Connectez-vous pour commenter.

Réponse acceptée

Abraham Boayue
Abraham Boayue le 8 Juin 2018
Modifié(e) : Abraham Boayue le 8 Juin 2018
Here are simplified versions of your functions.
function Ep = energy(k,x)
% Calculates the potential energy
Ep = 1/2*k*x.^2;
end
function [v, Ek]= Kenergy(m,Ep)
% Calculates the velocity and the kenitic energy using the potential
% energy from the first function
v = sqrt(2*Ep/m);
Ek = 1/2*m* v.^2;
end
clear variables
close all
% Use this m-file to get your results.
k = 0.1; % k = 10 N/cm = 10N/100m = 0.1N/m
m = 0.01;
dx = 0.5; % use dx = 0.005 to get a better graph of Ek
x1 = 0.5;
x2 = 2.5;
x = x1:dx:x2;
x = x/100; % convert x to meter
% Part 1
Ep = energy(k,x);
% Part 2
[v, Ek]= Kenergy(m,Ep);
% Part 3
figure
subplot(121)
plot(v,Ek,'LineWidth',2);
a= title('Graph of the Kenitic Energy: Ek vs v');
set(a,'fontsize',14);
a= xlabel('Velocity');
set(a,'fontsize',20);
a = ylabel('Ek');
set(a,'fontsize',20);
grid
subplot(122)
plot(x,v,'LineWidth',2,'color','m');
a= title('Graph of the velocity: v vs x');
set(a,'fontsize',14);
a= xlabel('x');
set(a,'fontsize',20);
a = ylabel('v');
set(a,'fontsize',20);
grid

Plus de réponses (1)

Abraham Boayue
Abraham Boayue le 6 Juin 2018
Are you trying to solve the equation Ep = Ek? Where Ep = mgh as the potential energy and Ek = 1/2mv^2 as kinetic energy. I see that you are using the calculated Ep to find v in the second function to calculate another Ek = 0.5m2v^2. Your approach is not quite clear, can you clarify a bit more.
  2 commentaires
Zachary Giovanelli
Zachary Giovanelli le 6 Juin 2018
Yes I am trying to find potential energy of the first problem (function) and then I am asking to find the kinetic energy of the second (function) along with the velocity of that same kinetic energy problem. So all I am giving are the equations and then the Spring constant which is 1000 Newtons per meter and then the compression distance of that spring which is .025 meters. Everything else I have to solve for in code as well as plot the data from .005 meters to .025 meters in increments of .005 meters. I can attach the actual questions I am being asked.
Zachary Giovanelli
Zachary Giovanelli le 6 Juin 2018
The reason why it is in meters is because I was asked to put the answers using meters.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by