MATLAB Answers

How can i optimize the code and connect the functions?

73 views (last 30 days)
Hi everyone!
I've been dabbling with MatLab for the past three days and 've managed to write the following, working, code.
eta_l = xlsread('calculos.xlsx','Theoretical Predictions','S2');
eta_0 = xlsread('calculos.xlsx','Theoretical Predictions','S3');
l_c = xlsread('calculos.xlsx','Theoretical Predictions','P3');
sigma_f = xlsread('calculos.xlsx','Base Values','B3');
v_f = xlsread('calculos.xlsx','Burn','E23');
E_m = xlsread('calculos.xlsx','Base Values','D2');
E_f = xlsread('calculos.xlsx','Base Values','B2');
d = xlsread('calculos.xlsx','Base Values','B5');
%basically these functions y1 and y2 should be connected but i haven't been able to connect them
%plot of the Kelly-Tyson equation
x=linspace(-10,50);
idx = x < l_c;
y1=((eta_l.*eta_0.*((v_f.*sigma_f.*E_m)./(E_f.*d.*sqrt(3))).*x(idx))+((sigma_f.*E_m.*(1-v_f))./E_f));
y2=(eta_l.*eta_0.*(((-sigma_f.*v_f.*l_c)./2).*(1./x(~idx))+(sigma_f.*v_f)))+(((sigma_f.*E_m)./E_f).*(1-v_f));
plot(x(idx),y1,x(~idx),y2);
ylim([0 1000]);
grid on
hold on
%basically these next two bits are the same and i would like to not repeat
%them and get the same to points in the plot
%plot of the 95% of the max of the Kelly-Tyson equation
syms x
f=(eta_l.*eta_0.*(((-sigma_f.*v_f.*l_c)./2).*(1./x)+(sigma_f.*v_f)))+(((sigma_f.*E_m)./E_f).*(1-v_f));
maximus=limit(f,Inf);
eqn=f==0.95.*maximus;
S=solve(eqn,x);
plot(S,0.95.*maximus,'r*')
hold on
%plot of the 98% of the max of Kelly-Tyson equation
syms x
f1=(eta_l.*eta_0.*(((-sigma_f.*v_f.*l_c)./2).*(1./x)+(sigma_f.*v_f)))+(((sigma_f.*E_m)./E_f).*(1-v_f));
maximus1=limit(f1,Inf);
eqn1=f1==0.98.*maximus1;
S1=solve(eqn1,x);
plot(S1,0.98.*maximus1,'r*')
hold off
But... The last two bits are basically me writing them again and again. Is there a way where i can optimize the code so that i get a plot with the graphs of the functions connected - that is because they are connected in reality - and the points ploted but without having to write basically the same thing twice?
Thanks in advance!

Accepted Answer

Star Strider
Star Strider on 14 Jul 2021
The syms call is not necessary.
Try something like this instead:
% syms x
f= @(x) (eta_l.*eta_0.*(((-sigma_f.*v_f.*l_c)./2).*(1./x)+(sigma_f.*v_f)))+(((sigma_f.*E_m)./E_f).*(1-v_f));
maximus = f(max(x));
eqn = @(x)f(x)*0.95.*maximus;
x0 = rand;
S=fsolve(eqn,x0);
plot(S,0.95.*maximus,'r*')
hold on
%plot of the 98% of the max of Kelly-Tyson equation
% syms x
f1= @(x) (eta_l.*eta_0.*(((-sigma_f.*v_f.*l_c)./2).*(1./x)+(sigma_f.*v_f)))+(((sigma_f.*E_m)./E_f).*(1-v_f));
maximus1=f1(max(x));
eqn1= @(x)f1(x)*0.98.*maximus1;
S1=fsolve(eqn1,x0);
plot(S1,0.98.*maximus1,'r*')
hold off
The functions each appear to be an inverse function of ‘x’, so will likely have only one root. (I cannot determine if the functions have a zero-crossing, so it is possible that there are no roots.) I am not certain what the limit call is doing, since the part of the function that contains the term will go to 0 in the limit, leaving only the additive terms that are not a function of ‘x’.
.
  6 Comments

Sign in to comment.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 14 Jul 2021
One quick and easy suggestion is to employ readtable() instead of xlsread().

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by