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 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Zachary Giovanelli
il 6 Giu 2018
Commentato: Olyver Fierro
il 3 Mar 2021
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 Commenti
Rik
il 6 Giu 2018
If the Kenergy function has an m-file of its own, then there are two options:
- it results in an error, as Penergy is not an internal Matlab function, nor is it declared as a variable before it is used
- 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.
Risposta accettata
Abraham Boayue
il 8 Giu 2018
Modificato: Abraham Boayue
il 8 Giu 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
Più risposte (1)
Abraham Boayue
il 6 Giu 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.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!