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.

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 comentarios

What is the error? Where is it occuring? Kindly explain it little more briefly.
It responds in the command window like this:
>> Kenergy(.005,.005,.025)
Out of memory. The likely cause is an infinite recursion within the program.
Error in Penergy (line 3)
v = sqrt((Penergy * 2)/.01);
How does Penergy get into the workspace of the Kenergy function? Is it a function somewhere else? Is it a variable in a nested function?
I am not sure exactly what you're asking. All the code that I typed in is what I have so everything is based on the code written.
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.
@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.

Iniciar sesión para comentar.

 Respuesta aceptada

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

Más respuestas (1)

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 comentarios

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.
The reason why it is in meters is because I was asked to put the answers using meters.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by