Effacer les filtres
Effacer les filtres

How i get a graph that i attached here with this matlab code?

40 vues (au cours des 30 derniers jours)
Sharqa
Sharqa le 16 Août 2024 à 14:30
Commenté : Cris LaPierre le 21 Août 2024 à 13:40
HTP()
phi = 0.1000
phi = 0.2000
phi = 0.3000
phi = 0.4000
function HTP()
clc
clear all format long % hybrid Carreau
% Define constants
J1 = 0.1;
J2 = 0.1;
J3 = 0.1;
J4 = 0.1;
JS = 0.1;
z = 0.1;
S = 0.1;
GC = 0.1;
Gr = 0.1;
H = 0.1;
a = 0.1;
m = 1;
G = 0.5;
phi = 0.1;
% Define time vector
t = linspace(0, 5, 100); % 100 points between 0 and 5
%t= 1;
% Calculate u1 and u2
u1 = exp(-t) - 1;
% Compute u2 with corrected parentheses and mathematical operations
exp_t = exp(t); % Compute exp(t) once for efficiency
term1 = -33 / J1 * (1 - exp_t - z / 3 * S);
term2 = 2 * GC * J3 + 2 * Gr * J2;
term3 = (2 * J4 * H * a^2) / (1 + m^2);
term4 = 3 * G * exp_t + 6 * exp_t / ((1 - phi)^2.5);
term5 = 2 * GC * JS * exp_t + 2 * Gr * J2 * exp_t;
u2 = (term1 + term2 - term3 - term4 - term5 + term3 * (exp_t - m + m * exp_t)) / (6 * a);
% Compute u
y = 1; % Define y as 1 or another constant value; adjust as needed
u = u1 * y + u2 * y.^2;
% Plot the result
phi = 0.1
figure ;
plot(t, u);
phi= 0.2
figure ;
plot(t, u);
phi = 0.3
figure ;
plot(t, u);
phi = 0.4
figure ;
plot(t, u);
xlabel('Time (t)');
ylabel('u(t)');
title('Plot of u(t)');
axis([0 5 min(u) max(u)]);
grid on;
end
  1 commentaire
Walter Roberson
Walter Roberson le 16 Août 2024 à 20:25
clc
clear all format long
First off, that should likely be
clear all
format long
Secondly: clear all within a function is a waste of time at best, and can lead to execution problems at worst. There is no need to clear all within a function.
I recommend against using clc within a function as well.

Connectez-vous pour commenter.

Réponses (2)

Star Strider
Star Strider le 16 Août 2024 à 14:49
One problem is that ‘term4’ and therefore ‘u2’ and ‘u’ should be functions of ‘phi’ and they are not originally. However changing that does not change the plots, leading me to believe that there is a coding error somewhere in ‘u’. You need to find it and fix it. Also, if you want all of the plots in the same axes, use the hold function rather than plotting them in individual figure objects.
HTP
Q = 1x100
1.0e+04 * -0.001368015498012 0.001395472200257 0.004302612268278 0.007360821699399 0.010577902910592 0.013962063648777 0.017521937931270 0.021266608073767 0.025205627862078 0.029349046926730 0.033707436382604 0.038291915799052 0.043114181569278 0.048186536751372 0.053521922457137 0.059133950868772 0.065036939967669 0.071245950063909 0.077776822219672 0.084646218664570 0.091871665306041 0.099471596443228 0.107465401798460 0.115873475986291 0.124717270546330 0.134019348672603 0.143803442779087 0.154094515048271 0.164918821117233 0.176303977063707
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
phi =
0.100000000000000
phi =
0.200000000000000
phi =
0.300000000000000
phi =
0.400000000000000
function HTP()
% clc
% clear all
format long % hybrid Carreau
% Define constants
J1 = 0.1;
J2 = 0.1;
J3 = 0.1;
J4 = 0.1;
JS = 0.1;
z = 0.1;
S = 0.1;
GC = 0.1;
Gr = 0.1;
H = 0.1;
a = 0.1;
m = 1;
G = 0.5;
phi = 0.1;
% Define time vector
t = linspace(0, 5, 100); % 100 points between 0 and 5
%t= 1;
% Calculate u1 and u2
u1 = exp(-t) - 1;
% Compute u2 with corrected parentheses and mathematical operations
exp_t = exp(t); % Compute exp(t) once for efficiency
term1 = -33 / J1 * (1 - exp_t - z / 3 * S);
term2 = 2 * GC * J3 + 2 * Gr * J2;
term3 = (2 * J4 * H * a^2) / (1 + m^2);
term4 = @(phi) 3 * G * exp_t + 6 * exp_t / ((1 - phi)^2.5);
term5 = 2 * GC * JS * exp_t + 2 * Gr * J2 * exp_t;
u2 = @(phi) (term1 + term2 - term3 - term4(phi) - term5 + term3 * (exp_t - m + m * exp_t)) / (6 * a);
% Compute u
y = 1; % Define y as 1 or another constant value; adjust as needed
u = @(phi) u1 * y + u2(phi) * y.^2;
% Plot the result
phi = 0.1
figure ;
plot(t, u(phi));
phi= 0.2
figure ;
plot(t, u(phi));
phi = 0.3
figure ;
plot(t, u(phi));
phi = 0.4
figure ;
plot(t, u(phi));
xlabel('Time (t)');
ylabel('u(t)');
title('Plot of u(t)');
axis([0 5 min(u(phi)) max(u(phi))]);
grid on;
end
.

Cris LaPierre
Cris LaPierre le 16 Août 2024 à 15:05
Right now, you just plot the same line 4 times: plot(t,u)
Changing the value of phi will not change the value of u. You must recalculate it. Here, I use anonymous functions for that purpose.
Also, use hold on and hold off without figure commands to add multiple lines to the same figure. See Ch 9 of MATLAB Onramp for more details.
Use text and/or annotation to add annotations to your plot.
% Define constants
J1 = 0.1;
J2 = 0.1;
J3 = 0.1;
J4 = 0.1;
JS = 0.1;
z = 0.1;
S = 0.1;
GC = 0.1;
Gr = 0.1;
H = 0.1;
a = 0.1;
m = 1;
G = 0.5;
% Define time vector
t = linspace(0, 5, 100); % 100 points between 0 and 5
% Calculate u1 and u2
u1 = exp(-t) - 1;
% Compute u2 with corrected parentheses and mathematical operations
exp_t = exp(t); % Compute exp(t) once for efficiency
term1 = -33 / J1 * (1 - exp_t - z / 3 * S);
term2 = 2 * GC * J3 + 2 * Gr * J2;
term3 = (2 * J4 * H * a^2) / (1 + m^2);
term4 = @(phi) 3 * G * exp_t + 6 * exp_t / ((1 - phi)^2.5);
term5 = 2 * GC * JS * exp_t + 2 * Gr * J2 * exp_t;
u2 = @(phi) (term1 + term2 - term3 - term4(phi) - term5 + term3 * (exp_t - m + m * exp_t)) / (6 * a);
% Compute u
y = 1; % Define y as 1 or another constant value; adjust as needed
u = @(phi) u1 * y + u2(phi) * y.^2;
% Plot the result
phi = 0.1
phi = 0.1000
figure ;
plot(t, u(phi),'k-');
hold on
phi= 0.2
phi = 0.2000
plot(t, u(phi),'k--');
phi = 0.3
phi = 0.3000
plot(t, u(phi),'k:');
phi = 0.4
phi = 0.4000
plot(t, u(phi),'k-.');
hold off
xlabel('Time (t)');
ylabel('u(t)');
title('Plot of u(t)');
axis([0 5 min(u(phi)) max(u(phi))]);
grid on;
legend("\phi=" + [0.1 0.2 0.3 0.4])
  2 commentaires
Sharqa
Sharqa le 21 Août 2024 à 8:52
Thankyou.
But,how i get this graph?
which parameter should i change ?
Cris LaPierre
Cris LaPierre le 21 Août 2024 à 13:40
Are you asking about the annotations or the shapes of the curve?
Have you gone through Ch 9 of MATLAB Onramp?

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by