Tag Archives: matlab
After adding a command to get dotted line graph I am still getting solid line only in the legend i am getting dotted line
plot(x1 – x2,F,’:g’);
xlabel(‘Displacement(m)’)
ylabel(‘Spring Force(N)’)
legend(‘F’)plot(x1 – x2,F,’:g’);
xlabel(‘Displacement(m)’)
ylabel(‘Spring Force(N)’)
legend(‘F’) plot(x1 – x2,F,’:g’);
xlabel(‘Displacement(m)’)
ylabel(‘Spring Force(N)’)
legend(‘F’) graph MATLAB Answers — New Questions
I have a nonlinear equation with a symbolic variable and cant solve it.
Hi, I have to plot a phase diagram of an implicit equation, but in there lies a function p that depends nonlinearly on one of the variables (alpha), so I am trying to solve the nonlinear equation in terms of my variable alpha. However, I only get a solution in terms of a z2 variable. Can anybody help? Here is the code
n = 2.1;
alpha0 = 0;
syms p alpha
eqn = p^(n+1) – alpha0*p^n + p -(alpha0 + alpha) == 0;
sol = solve(eqn,p);
For n = 2 works just fine, but not for n = 2.1, which prompts my problem.Hi, I have to plot a phase diagram of an implicit equation, but in there lies a function p that depends nonlinearly on one of the variables (alpha), so I am trying to solve the nonlinear equation in terms of my variable alpha. However, I only get a solution in terms of a z2 variable. Can anybody help? Here is the code
n = 2.1;
alpha0 = 0;
syms p alpha
eqn = p^(n+1) – alpha0*p^n + p -(alpha0 + alpha) == 0;
sol = solve(eqn,p);
For n = 2 works just fine, but not for n = 2.1, which prompts my problem. Hi, I have to plot a phase diagram of an implicit equation, but in there lies a function p that depends nonlinearly on one of the variables (alpha), so I am trying to solve the nonlinear equation in terms of my variable alpha. However, I only get a solution in terms of a z2 variable. Can anybody help? Here is the code
n = 2.1;
alpha0 = 0;
syms p alpha
eqn = p^(n+1) – alpha0*p^n + p -(alpha0 + alpha) == 0;
sol = solve(eqn,p);
For n = 2 works just fine, but not for n = 2.1, which prompts my problem. nonlinear, nonlinear equation, symbolic variable MATLAB Answers — New Questions
Resetting only a section of my state during event driven ode45 simulation
Hello,
I’m trying to write a code that simulates a closed loop event driven LTI system (with a poleplacement controller u(t) = -K*x(t)). I’m using the following event formulation:
where the stands for the actual state and the stands for the error between the current state and the state measured during an event trigger. So:
So I want to trigger an event the moment the value becomes 0.
Now, the problem I’m having is that I want the value (so the error) to reset to 0 upon an event but the part of the state should not reset. I’m having trouble coming up with a way to exactly program this. I’m currently just simulating the system dynamics for the augmented system, so for . For now I have the following in matlab:
a = 4;
b = 6;
c = 9;
poles = [-1+2i; -1-2i];
A = [0.3+a-b, 0.5-c; 0, 1];
B = [0; 1];
C = eye(2);
D = [0; 0];
K = place(A, B, poles);
A_aug = [A-B*K -B*K;
-A+B*K B*K];%Defining augmented CL A-matrix for augmented state x_augmented = [x;error]//[xi;epsilon]
tspan = [0,10];
x0 = [1;1;0;0];
Q = eye(2);
P = 2*eye(2);
sig = 0.1;
options = odeset(‘Events’, @(t,x) myEventsFcn(t,x,sig,Q,P,B,K));
[t,y,te,ye,ie] = ode45(@(t,x) LTIaug_fun(t,x,A_aug),tspan,x0,options)
Where the LTIaug_fun and myEventsFcn are defined as follows:
function dxdt = LTIaug_fun(~,x,A_aug)
dxdt = A_aug*x;
end
function [value,isterminal,direction] = myEventsFcn(t,x,sig,Q,P,B,K)
Th = [(1-sig)*Q P*B*K;
(B*K)’*P zeros(2,2)];
Th_e = x’*Th*x;
value = Th_e;
isterminal = 1;
direction = 0;
end
So how do I set my part of my augmented state to 0 after/during an event trigger?Hello,
I’m trying to write a code that simulates a closed loop event driven LTI system (with a poleplacement controller u(t) = -K*x(t)). I’m using the following event formulation:
where the stands for the actual state and the stands for the error between the current state and the state measured during an event trigger. So:
So I want to trigger an event the moment the value becomes 0.
Now, the problem I’m having is that I want the value (so the error) to reset to 0 upon an event but the part of the state should not reset. I’m having trouble coming up with a way to exactly program this. I’m currently just simulating the system dynamics for the augmented system, so for . For now I have the following in matlab:
a = 4;
b = 6;
c = 9;
poles = [-1+2i; -1-2i];
A = [0.3+a-b, 0.5-c; 0, 1];
B = [0; 1];
C = eye(2);
D = [0; 0];
K = place(A, B, poles);
A_aug = [A-B*K -B*K;
-A+B*K B*K];%Defining augmented CL A-matrix for augmented state x_augmented = [x;error]//[xi;epsilon]
tspan = [0,10];
x0 = [1;1;0;0];
Q = eye(2);
P = 2*eye(2);
sig = 0.1;
options = odeset(‘Events’, @(t,x) myEventsFcn(t,x,sig,Q,P,B,K));
[t,y,te,ye,ie] = ode45(@(t,x) LTIaug_fun(t,x,A_aug),tspan,x0,options)
Where the LTIaug_fun and myEventsFcn are defined as follows:
function dxdt = LTIaug_fun(~,x,A_aug)
dxdt = A_aug*x;
end
function [value,isterminal,direction] = myEventsFcn(t,x,sig,Q,P,B,K)
Th = [(1-sig)*Q P*B*K;
(B*K)’*P zeros(2,2)];
Th_e = x’*Th*x;
value = Th_e;
isterminal = 1;
direction = 0;
end
So how do I set my part of my augmented state to 0 after/during an event trigger? Hello,
I’m trying to write a code that simulates a closed loop event driven LTI system (with a poleplacement controller u(t) = -K*x(t)). I’m using the following event formulation:
where the stands for the actual state and the stands for the error between the current state and the state measured during an event trigger. So:
So I want to trigger an event the moment the value becomes 0.
Now, the problem I’m having is that I want the value (so the error) to reset to 0 upon an event but the part of the state should not reset. I’m having trouble coming up with a way to exactly program this. I’m currently just simulating the system dynamics for the augmented system, so for . For now I have the following in matlab:
a = 4;
b = 6;
c = 9;
poles = [-1+2i; -1-2i];
A = [0.3+a-b, 0.5-c; 0, 1];
B = [0; 1];
C = eye(2);
D = [0; 0];
K = place(A, B, poles);
A_aug = [A-B*K -B*K;
-A+B*K B*K];%Defining augmented CL A-matrix for augmented state x_augmented = [x;error]//[xi;epsilon]
tspan = [0,10];
x0 = [1;1;0;0];
Q = eye(2);
P = 2*eye(2);
sig = 0.1;
options = odeset(‘Events’, @(t,x) myEventsFcn(t,x,sig,Q,P,B,K));
[t,y,te,ye,ie] = ode45(@(t,x) LTIaug_fun(t,x,A_aug),tspan,x0,options)
Where the LTIaug_fun and myEventsFcn are defined as follows:
function dxdt = LTIaug_fun(~,x,A_aug)
dxdt = A_aug*x;
end
function [value,isterminal,direction] = myEventsFcn(t,x,sig,Q,P,B,K)
Th = [(1-sig)*Q P*B*K;
(B*K)’*P zeros(2,2)];
Th_e = x’*Th*x;
value = Th_e;
isterminal = 1;
direction = 0;
end
So how do I set my part of my augmented state to 0 after/during an event trigger? ode45, event based control MATLAB Answers — New Questions
ADI method for 2D reaction diffusion equation
hi , i want to use ADI mathod to fisherkpp equation and calcultae the integral between t=0 and t=10 however im not sure that i apply the method correctly ? so i want your feedbacks and your suggestion sto improve the code
here the code
% Parameters
Lx = 60; % Length of domain in x-direction
Ly = 1; % Length of domain in y-direction
Nx = 600; % Number of grid points in x-direction
Ny = 10; % Number of grid points in y-direction
final_time = 10; % Final time
D = 1; % Diffusion coefficient
r = 1; % Growth rate
% Discretization
dx = Lx / (Nx – 1);
dy = Ly / (Ny – 1);
dt = 0.006; % Time step
Nt = 5500; % Number of time steps
x = linspace(0, Lx, Nx);
y = linspace(0, Ly, Ny);
% Initial condition
[X, Y] = meshgrid(x, y);
u0 = (1/2 – 1/2 * tanh(1/(2*sqrt(6)) * X)).^2;
% ADI method
alpha = D * dt / dx^2;
beta = D * dt / dy^2;
gamma = r * dt;
u = u0;
u_new = u;
integral_u = 0;
for t = 1:Nt
% Implicit step in x-direction
for j = 2:Ny-1
% Construct tridiagonal matrix for x-direction
A = -alpha * ones(1, Nx-3);
B = (1 + 2 * alpha) * ones(1, Nx-2);
C = -alpha * ones(1, Nx-3);
D_vec = u(j, 2:end-1) + gamma * u(j, 2:end-1) .* (1 – u(j, 2:end-1));
% Solve tridiagonal system
u_new(j, 2:end-1) = tridiag_solve(A, B, C, D_vec);
end
% Implicit step in y-direction
for i = 2:Nx-1
% Construct tridiagonal matrix for y-direction
A = -beta * ones(1, Ny-3);
B = (1 + 2 * beta) * ones(1, Ny-2);
C = -beta * ones(1, Ny-3);
D_vec = u_new(2:end-1, i) + gamma * u_new(2:end-1, i) .* (1 – u_new(2:end-1, i));
% Solve tridiagonal system
u_new(2:end-1, i) = tridiag_solve(A, B, C, D_vec);
end
% Update integral of u over domain
if t > 1
integral_u = integral_u + trapz(y, trapz(x, u_new, 2), 1) * dt; % Trapezoidal numerical integration
end
u = u_new;
end
disp([‘Integral of u(x, y) over domain between t=0 and final time: ‘, num2str(integral_u)]);
function x = tridiag_solve(A, B, C, D)
% Solves a tridiagonal system of equations Ax = D
N = length(B);
c = zeros(N-1, 1);
d = zeros(N, 1);
x = zeros(N, 1);
c(1) = C(1) / B(1);
d(1) = D(1) / B(1);
for i = 2:N-1
denom = B(i) – A(i-1) * c(i-1);
c(i) = C(i) / denom;
d(i) = (D(i) – A(i-1) * d(i-1)) / denom;
end
d(N) = (D(N) – A(N-1) * d(N-1)) / (B(N) – A(N-1) * c(N-1));
x(N) = d(N);
for i = N-1:-1:1
x(i) = d(i) – c(i) * x(i+1);
end
endhi , i want to use ADI mathod to fisherkpp equation and calcultae the integral between t=0 and t=10 however im not sure that i apply the method correctly ? so i want your feedbacks and your suggestion sto improve the code
here the code
% Parameters
Lx = 60; % Length of domain in x-direction
Ly = 1; % Length of domain in y-direction
Nx = 600; % Number of grid points in x-direction
Ny = 10; % Number of grid points in y-direction
final_time = 10; % Final time
D = 1; % Diffusion coefficient
r = 1; % Growth rate
% Discretization
dx = Lx / (Nx – 1);
dy = Ly / (Ny – 1);
dt = 0.006; % Time step
Nt = 5500; % Number of time steps
x = linspace(0, Lx, Nx);
y = linspace(0, Ly, Ny);
% Initial condition
[X, Y] = meshgrid(x, y);
u0 = (1/2 – 1/2 * tanh(1/(2*sqrt(6)) * X)).^2;
% ADI method
alpha = D * dt / dx^2;
beta = D * dt / dy^2;
gamma = r * dt;
u = u0;
u_new = u;
integral_u = 0;
for t = 1:Nt
% Implicit step in x-direction
for j = 2:Ny-1
% Construct tridiagonal matrix for x-direction
A = -alpha * ones(1, Nx-3);
B = (1 + 2 * alpha) * ones(1, Nx-2);
C = -alpha * ones(1, Nx-3);
D_vec = u(j, 2:end-1) + gamma * u(j, 2:end-1) .* (1 – u(j, 2:end-1));
% Solve tridiagonal system
u_new(j, 2:end-1) = tridiag_solve(A, B, C, D_vec);
end
% Implicit step in y-direction
for i = 2:Nx-1
% Construct tridiagonal matrix for y-direction
A = -beta * ones(1, Ny-3);
B = (1 + 2 * beta) * ones(1, Ny-2);
C = -beta * ones(1, Ny-3);
D_vec = u_new(2:end-1, i) + gamma * u_new(2:end-1, i) .* (1 – u_new(2:end-1, i));
% Solve tridiagonal system
u_new(2:end-1, i) = tridiag_solve(A, B, C, D_vec);
end
% Update integral of u over domain
if t > 1
integral_u = integral_u + trapz(y, trapz(x, u_new, 2), 1) * dt; % Trapezoidal numerical integration
end
u = u_new;
end
disp([‘Integral of u(x, y) over domain between t=0 and final time: ‘, num2str(integral_u)]);
function x = tridiag_solve(A, B, C, D)
% Solves a tridiagonal system of equations Ax = D
N = length(B);
c = zeros(N-1, 1);
d = zeros(N, 1);
x = zeros(N, 1);
c(1) = C(1) / B(1);
d(1) = D(1) / B(1);
for i = 2:N-1
denom = B(i) – A(i-1) * c(i-1);
c(i) = C(i) / denom;
d(i) = (D(i) – A(i-1) * d(i-1)) / denom;
end
d(N) = (D(N) – A(N-1) * d(N-1)) / (B(N) – A(N-1) * c(N-1));
x(N) = d(N);
for i = N-1:-1:1
x(i) = d(i) – c(i) * x(i+1);
end
end hi , i want to use ADI mathod to fisherkpp equation and calcultae the integral between t=0 and t=10 however im not sure that i apply the method correctly ? so i want your feedbacks and your suggestion sto improve the code
here the code
% Parameters
Lx = 60; % Length of domain in x-direction
Ly = 1; % Length of domain in y-direction
Nx = 600; % Number of grid points in x-direction
Ny = 10; % Number of grid points in y-direction
final_time = 10; % Final time
D = 1; % Diffusion coefficient
r = 1; % Growth rate
% Discretization
dx = Lx / (Nx – 1);
dy = Ly / (Ny – 1);
dt = 0.006; % Time step
Nt = 5500; % Number of time steps
x = linspace(0, Lx, Nx);
y = linspace(0, Ly, Ny);
% Initial condition
[X, Y] = meshgrid(x, y);
u0 = (1/2 – 1/2 * tanh(1/(2*sqrt(6)) * X)).^2;
% ADI method
alpha = D * dt / dx^2;
beta = D * dt / dy^2;
gamma = r * dt;
u = u0;
u_new = u;
integral_u = 0;
for t = 1:Nt
% Implicit step in x-direction
for j = 2:Ny-1
% Construct tridiagonal matrix for x-direction
A = -alpha * ones(1, Nx-3);
B = (1 + 2 * alpha) * ones(1, Nx-2);
C = -alpha * ones(1, Nx-3);
D_vec = u(j, 2:end-1) + gamma * u(j, 2:end-1) .* (1 – u(j, 2:end-1));
% Solve tridiagonal system
u_new(j, 2:end-1) = tridiag_solve(A, B, C, D_vec);
end
% Implicit step in y-direction
for i = 2:Nx-1
% Construct tridiagonal matrix for y-direction
A = -beta * ones(1, Ny-3);
B = (1 + 2 * beta) * ones(1, Ny-2);
C = -beta * ones(1, Ny-3);
D_vec = u_new(2:end-1, i) + gamma * u_new(2:end-1, i) .* (1 – u_new(2:end-1, i));
% Solve tridiagonal system
u_new(2:end-1, i) = tridiag_solve(A, B, C, D_vec);
end
% Update integral of u over domain
if t > 1
integral_u = integral_u + trapz(y, trapz(x, u_new, 2), 1) * dt; % Trapezoidal numerical integration
end
u = u_new;
end
disp([‘Integral of u(x, y) over domain between t=0 and final time: ‘, num2str(integral_u)]);
function x = tridiag_solve(A, B, C, D)
% Solves a tridiagonal system of equations Ax = D
N = length(B);
c = zeros(N-1, 1);
d = zeros(N, 1);
x = zeros(N, 1);
c(1) = C(1) / B(1);
d(1) = D(1) / B(1);
for i = 2:N-1
denom = B(i) – A(i-1) * c(i-1);
c(i) = C(i) / denom;
d(i) = (D(i) – A(i-1) * d(i-1)) / denom;
end
d(N) = (D(N) – A(N-1) * d(N-1)) / (B(N) – A(N-1) * c(N-1));
x(N) = d(N);
for i = N-1:-1:1
x(i) = d(i) – c(i) * x(i+1);
end
end numerical integration, numerical analysis MATLAB Answers — New Questions
Curve Fitting for a Rational Polynomial Model.
Dear all,
I want to find the best rational Polynomial model that can fit the data shown.
any help would be appreciated.
Data are in dB.Dear all,
I want to find the best rational Polynomial model that can fit the data shown.
any help would be appreciated.
Data are in dB. Dear all,
I want to find the best rational Polynomial model that can fit the data shown.
any help would be appreciated.
Data are in dB. curve fitting MATLAB Answers — New Questions