MATLAB Answers

How do I use output values from a function to plot those values in a script

3 ビュー (過去 30 日間)
Nolan Hensley
Nolan Hensley 2021 年 9 月 13 日
コメント済み: Nolan Hensley 2021 年 9 月 13 日
I want to call my function (in the script) and plot the vectors from specfic outputs of the function.
Function code: (this seems to be working perfectly as it is meant to output a time, velocity, and mass vector given: delt (step size) and t_end (ending time).
function [t,m,v] = rocket_euler(delt,t_end) %rocket_euler solves for t,v,m vectors using Euler's method
g = 9.81; %gravity (m/s^2)
c = 25000; %thrust constant (m/s)
t = 0:delt:t_end; %time vector from 0 to t_end seconds
t(1) = 0; %initial time set to 0 (s)
m(1) = 1000000; %initial mass of fuel (kg)
v(1) = 0; %initial velocity of rocket (m/s)
for ii=1:length(t)-1
dmdt = -4000; %fuel burn rate (kg/s)
m(ii+1) = m(ii) + (dmdt*delt); %update law for Euler's Method
dvdt = -g - ((c*dmdt)/(m(ii))); %solve given formula for dvdt or acceleration
v(ii+1) = v(ii) + (dvdt*delt); %update law for Euler's Method
end
end
Here is my script: (120s is always the value used for t_end while delt (step size) changes from 60 to 30 to 15 and to 5) I want to plot the different lines it makes (due to changes in step size) on a single plot.
clc; clear; %clears workspace and command window
plot(t = rocket_euler(60,120),v = rocket_euler(60,120),'.-','color',[0 0 1],'Marker','*','markersize',15,'linewidth',1.5) %plot t vs v
legend('Step Size: 60s'); %step size of 60s
hold on %prevent overwrite of plot
plot(t = rocket_euler(30,120),v = rocket_euler(30,120) ,'.-','color',[0 1 0],'Marker','x','markersize',15,'linewidth',1.5) %plot t vs v
legend('Step Size: 30s'); %step size of 30s
hold on %prevent overwrite of plot
plot(t = rocket_euler(15,120),v = rocket_euler(15,120) ,'.-','color',[1 0 0],'Marker','o','markersize',15,'linewidth',1.5) %plot t vs v
legend('Step Size: 15s'); %step size of 15s
hold on %prevent overwrite of plot
plot(t = rocket_euler(5,120),v = rocket_euler(5,120) ,'.-','color',[0 1 1],'Marker','+','markersize',15,'linewidth',1.5) %plot t vs v
legend('Step Size: 5s'); %step size of 5s
xlabel('t (seconds)') %add x-label
ylabel('v (m/s)') %add y-label
t = rocket_euler(60,120),v = rocket_euler(60,120)
The above snippet of code is where I believe I am going wrong. Same for each line that begins with "plot(..."

採用された回答

Dave B
Dave B 2021 年 9 月 13 日
編集済み: Dave B 2021 年 9 月 13 日
I think what you're looking for is:
[t,~,v] = rocket_euler(30,20)
plot(t, v, '.-','color',[0 1 0],'Marker','x','markersize',15,'linewidth',1.5) %plot t vs v
You can't pass in the positional arguments to plot using = that way, and I guessed that you wanted the first and third outputs from rocket_euler (otherwise you'd be getting the first output twice and plotting it against itself). I'm not sure that you had a use for the second argument, so I used ~ to say 'ignore it'
  1 件のコメント
Nolan Hensley
Nolan Hensley 2021 年 9 月 13 日
Yes this is exactly what I was looking for. Thank you

サインインしてコメントする。

その他の回答 (0 件)

製品

Community Treasure Hunt

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

Start Hunting!

Translated by