MATLAB Answers

I am trying to get both of my functions to be generated on one plot, but when my plot generates it goes through the first function within a few seconds but then only displays the second function. How can I make my plot have both functions?

1 view (last 30 days)
Cesar Rubio
Cesar Rubio on 25 Sep 2019
Answered: Milan Padhiyar on 27 Oct 2020
function hw2_1_3
clear all
close all
Dp = 5*10^-2; %particle diameter (m)
h = 1*10^-3; %fluid viscosity (1cp)
row = 2*10^3; %fluid density (was g/cm^3)
dP = 416; %pressure drop (Pa)
L = 1.5; %reactor length (m)
Vo = 0.1; %superficial velocity (m/s)
%eps = void fraction (epsilon), what we are trying to find
%1st way
fun1 = @(eps) dP/L - 150*(h*Vo/(Dp^2))... function
*((1 - eps)^2/(eps^3)) - (7/4)*((row*(Vo^2))/Dp)*((1 -eps)/eps^3);
x0 = [0.05 1]; %initial interval
options1 = optimset('PlotFcns',{@optimplotfval}); %plot iterations
[eps1, residual1, exitflag1, output1] = fzero(fun1,x0,options1)
%ep1 = epsilon = zereo in interval
%residual = f(x) (this is the number that will be closest to zero)
%2nd way
fun2 = @(eps) (dP/L)*eps^3 - 150*(h*Vo/(Dp^2))*((1 - eps)^2)...
x1 = [0.05 1]; %initial interval
options2 = optimset('PlotFcns',{@optimplotfval}); %plot iterations
[eps2, residual2, exitflag2, output2] = fzero(fun2,x1,options2)
%eps2 = epsilon = zereo in interval
%residual = f(x)

Answers (1)

Milan Padhiyar
Milan Padhiyar on 27 Oct 2020
Hi Cesar,
Here you are solving two equations individually using the “fzero” function. So, we cannot hold the previous plot values in this case while it starts execution for the second equation (fun2).
The alternative way is to store the X and Y data after the completion of each execution of “fzero” and then plot the data on the new figure together on the same axis.
To store the X and Y data together we can use following commands accordingly after each “fzero” solution,
h1 = findobj(gca,'Type','line');
h1x = get(h1,'Xdata');
h1y = get(h1,'Ydata');
Note: similarly store data for the second case (fun2)
Now, at the end we can create the new figure and plot the data on the same axis.
hold on
ylabel('Function value')
I hope this help you to resolve your query.

Community Treasure Hunt

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

Start Hunting!

Translated by