5 outputs of a function, store each output data in 5 arrays from for loops

3 Ansichten (letzte 30 Tage)
Hello
Need some help cleaning this up. Basically the program involves having 8 inputs and 5 outputs. Keep 7 of the 8 inputs constant and vary 1 of the constants (Angle Theta) from 1 to 90 degrees. This will give 90 different values for each of the 5 outputs. Then I want to store the data from each output into five 90x1 arrays and also make an extra 90x1 array storing the angles 1 to 90 (we'll call that array t)
i then want to plot t vs volume, t vs area, t vs up,t vs dV, t vs dA
what I did because i am not experienced, is make 5 different functions (one for each output) and one script file which has 5 for loops to call each function and make an array for each call. Then i plot 5 figures in that script. All of the code is shown below. What I'd like to do is combine the 5 function files into one and just call that one function from the script and have it show me the 5 plots.
i've tried to combine the 5 function files into one but then i'd have to declare the function as
function [V,A,Up,dA,dV] = Assignment1(Theta,B,L,r,R,Ach,Ap,N)
so when i'd call the function from my script in a for loop , I don't know how to return 90 values for each output and then plot the 5 graphs.
function [V] = VolumeFunction(Theta,B,L,r,R,Ach,Ap,N)
Vc=269.4
V = (1+(0.5*(r-0.5))*(R + 1 - cosd(Theta) - (R^2 - (sind(Theta))^2)^0.5))*Vc
%A= Ach + Ap + ((pi.*B.*L)/2).*(R+1 - cos(Theta) + sqrt(R.^2 - (sin(Theta).^2)))
%Up= pi.*L.*N.*sin(Theta) .* ( (cos(Theta)/(R.^2 - sin(Theta).^2) + 1))
%dV= (Vc.*(r-1))/2 .*(0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
%dA= (pi.*B*L)/2 .* (0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
end
function [A] = AreaFunction(Theta,B,L,r,R,Ach,Ap,N)
Vc=269.4
%V = (1+(0.5*(r-0.5))*(R + 1 - cosd(Theta) - (R^2 - (sind(Theta))^2)^0.5))*Vc
A= Ach + Ap + ((pi.*B.*L)/2).*(R+1 - cos(Theta) + sqrt(R.^2 - (sin(Theta).^2)))
%Up= pi.*L.*N.*sin(Theta) .* ( (cos(Theta)/(R.^2 - sin(Theta).^2) + 1))
%dV= (Vc.*(r-1))/2 .*(0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
%dA= (pi.*B*L)/2 .* (0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
end
function [Up] = UpFunction(Theta,B,L,r,R,Ach,Ap,N)
Vc=269.4
%V = (1+(0.5*(r-0.5))*(R + 1 - cosd(Theta) - (R^2 - (sind(Theta))^2)^0.5))*Vc
%A= Ach + Ap + ((pi.*B.*L)/2).*(R+1 - cos(Theta) + sqrt(R.^2 - (sin(Theta).^2)))
Up= pi.*L.*N.*sin(Theta) .* ( (cos(Theta)/(R.^2 - sin(Theta).^2) + 1))
%dV= (Vc.*(r-1))/2 .*(0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
%dA= (pi.*B*L)/2 .* (0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
end
function [dV] = dVFunction(Theta,B,L,r,R,Ach,Ap,N)
Vc=269.4
%V = (1+(0.5*(r-0.5))*(R + 1 - cosd(Theta) - (R^2 - (sind(Theta))^2)^0.5))*Vc
%A= Ach + Ap + ((pi.*B.*L)/2).*(R+1 - cos(Theta) + sqrt(R.^2 - (sin(Theta).^2)))
%Up= pi.*L.*N.*sin(Theta) .* ( (cos(Theta)/(R.^2 - sin(Theta).^2) + 1))
dV= (Vc.*(r-1))/2 .*(0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
%dA= (pi.*B*L)/2 .* (0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
end
function [dA] = dAFunction(Theta,B,L,r,R,Ach,Ap,N)
Vc=269.4
%V = (1+(0.5*(r-0.5))*(R + 1 - cosd(Theta) - (R^2 - (sind(Theta))^2)^0.5))*Vc
%A= Ach + Ap + ((pi.*B.*L)/2).*(R+1 - cos(Theta) + sqrt(R.^2 - (sin(Theta).^2)))
%Up= pi.*L.*N.*sin(Theta) .* ( (cos(Theta)/(R.^2 - sin(Theta).^2) + 1))
%dV= (Vc.*(r-1))/2 .*(0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
dA= (pi.*B*L)/2 .* (0.5.*(R.^2 - (sin(Theta)).^2).^-0.5 .* (2.*R-2.*sin(Theta).*cos(Theta)))
end
%Graphing Script below
B=14;
L=14;
r=9;
R=6;
Ach=231;
Ap=154;
N=1500;
Vc=269.4;
Theta=0;
t=zeros(90,1); %angle array
x=zeros(90,1); %V array
u=zeros(90,1);%A array
v=zeros(90,1);%Up array
w=zeros(90,1); %dV array
y=zeros(90,1); %dA array
for i=1:90 %Creating a theta array storing values 1 to 90
t(i)=i;
end
for Theta=1:90 %getting and storing V data
x(Theta) = VolumeFunction(Theta,B,L,r,R,Ach,Ap,N);
end
for Theta=1:90 % getting and storing A data
u(Theta) = AreaFunction(Theta,B,L,r,R,Ach,Ap,N);
end
for Theta=1:90 %getting and storing Up data
v(Theta) =UpFunction(Theta,B,L,r,R,Ach,Ap,N);
end
for Theta=1:90 % getting and storing dV data
w(Theta) =dVFunction(Theta,B,L,r,R,Ach,Ap,N);
end
for Theta=1:90 % getting and storing dA data
y(Theta) =dAFunction(Theta,B,L,r,R,Ach,Ap,N);
end
figure(1)
plot(t,x)
figure(2)
plot(t,u)
figure(3)
plot(t,v)
figure(4)
plot(t,w)
figure(5)
plot(t,y)
Any help is appreciated on making the 5 function files into 1. Thank you
  1 Kommentar
Stephen23
Stephen23 am 27 Jan. 2019
Bearbeitet: Stephen23 am 27 Jan. 2019
"...but then i'd have to declare the function as"
function [V,A,Up,dA,dV] = Assignment1(Theta,B,L,r,R,Ach,Ap,N)
"so when i'd call the function from my script in a for loop , I don't know how to return 90 values for each output and then plot the 5 graphs."
Before your editted your question you showed one function with five output arguments. What was wrong with that? Creating five separate functions is not the solution! My answer shows how easy it is to return multiple outputs from one function.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 27 Jan. 2019
Bearbeitet: Stephen23 am 27 Jan. 2019
% parameters:
B = 14;
L = 14;
r = 9;
R = 6; % bad idea to use variable names that differ only by case
Ach = 231;
Ap = 154;
N = 1500;
Vc = 269.4;
Theta = 1:90;
% outputs:
num = numel(Theta);
V = nan(num,1);
A = nan(num,1);
Up = nan(num,1);
dV = nan(num,1);
dA = nan(num,1);
for k = 1:num % better to loop over indices
[V(k),A(k),Up(k),dV(k),dA(k)] = Assignment1(Theta(k),B,L,r,R,Ach,Ap,N);
end
plot(Theta,[V,A,Up,dV,dA])

Weitere Antworten (0)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by