I have given matrix and function to solve ode45 .here 'x' and 'f' is a column 20*1 vector. xprime is a derivative of x.please help me the code to complete .
Show older comments
##matrix M is here,
clc;
clear all;
n = 20;
C= zeros(n, n);
C(1,n)=1;
C(1,n-1)=1;
C(2,n)=1;
A = zeros(n, n);
for i = 1:n
for j = 1:n
if j==i+1 || j == i + 2
A(i,j) = 1;
else
A(i, j) = 0;
end
end
end
B1=A+C;
B=B1+B1';
% Find non-zero and zero indices in the upper triangular part of the matrix
nonZeroIndices = []; % Initialize an array to store non-zero indices
zeroIndices = []; % Initialize an array to store zero indices
for i = 1:n
for j = i+1:n
if B1(i, j) ~= 0
nonZeroIndices = [nonZeroIndices; i, j];
else
zeroIndices = [zeroIndices; i, j];
end
end
end
% Shuffle the non-zero and zero indices randomly
shuffledNonZeroIndices = nonZeroIndices(randperm(size(nonZeroIndices, 1)), :);
shuffledZeroIndices = zeroIndices(randperm(size(zeroIndices, 1)), :);
% Choose the first two non-zero and zero indices from the shuffled arrays
numChosen = 2;
chosenNonZeroIndices = shuffledNonZeroIndices(1:numChosen, :);
chosenZeroIndices = shuffledZeroIndices(1:numChosen, :);
% % Define values to replace the chosen positions
% replacementValueNonZero = 999; % Replace chosen non-zero positions with this value
% replacementValueZero = -999; % Replace chosen zero positions with this value
% Replace the chosen non-zero positions with the replacement value
for i = 1:2
B1(chosenNonZeroIndices(i, 1), chosenNonZeroIndices(i, 2)) = 0;
end
% Replace the chosen zero positions with the replacement value
for i = 1:2
B1(chosenZeroIndices(i, 1), chosenZeroIndices(i, 2)) = 1;
end
% Display the modified matrix B
disp("Modified Matrix B1:");
disp(A);
M=B1+B1';
disp(M)
%G=graph(M)
%total=nnz(M);
%plot(G)
%%
function
function xprime = fun(t, x)
theta = 0.1;dh = 0.2;phi = 0.3;ita = 0.15;
nn=20;
i = 1; f = [];
while i< nn
f = [f; x(i)*(1 - theta*x(i)) - (x(i)*x(i+1))/(1 + x(i));(phi*x(i)*x(i+1))/(1 + x(i)) - ita*x(i+1)];
i = i + 1;
end
xprime = f+dh*K*x;
end
%%
x0 = rand(20,1);
tspan=[0 1000];
[t,x] = ode45(funnn,tspan, x0);
2 Comments
Star Strider
on 20 Aug 2023
The function needs to have ‘K’ passed to it, however I cannot determine what ‘K’ is here. The function also needs to be at the end of the code, after the ode45 call.
n = 20;
C= zeros(n, n);
C(1,n)=1;
C(1,n-1)=1;
C(2,n)=1;
A = zeros(n, n);
for i = 1:n
for j = 1:n
if j==i+1 || j == i + 2
A(i,j) = 1;
else
A(i, j) = 0;
end
end
end
B1=A+C;
B=B1+B1';
% Find non-zero and zero indices in the upper triangular part of the matrix
nonZeroIndices = []; % Initialize an array to store non-zero indices
zeroIndices = []; % Initialize an array to store zero indices
for i = 1:n
for j = i+1:n
if B1(i, j) ~= 0
nonZeroIndices = [nonZeroIndices; i, j];
else
zeroIndices = [zeroIndices; i, j];
end
end
end
% Shuffle the non-zero and zero indices randomly
shuffledNonZeroIndices = nonZeroIndices(randperm(size(nonZeroIndices, 1)), :);
shuffledZeroIndices = zeroIndices(randperm(size(zeroIndices, 1)), :);
% Choose the first two non-zero and zero indices from the shuffled arrays
numChosen = 2;
chosenNonZeroIndices = shuffledNonZeroIndices(1:numChosen, :);
chosenZeroIndices = shuffledZeroIndices(1:numChosen, :);
% % Define values to replace the chosen positions
% replacementValueNonZero = 999; % Replace chosen non-zero positions with this value
% replacementValueZero = -999; % Replace chosen zero positions with this value
% Replace the chosen non-zero positions with the replacement value
for i = 1:2
B1(chosenNonZeroIndices(i, 1), chosenNonZeroIndices(i, 2)) = 0;
end
% Replace the chosen zero positions with the replacement value
for i = 1:2
B1(chosenZeroIndices(i, 1), chosenZeroIndices(i, 2)) = 1;
end
% Display the modified matrix B
disp("Modified Matrix B1:");
disp(A);
M=B1+B1';
disp(M)
%G=graph(M)
%total=nnz(M);
%plot(G)
%%
% function
x0 = rand(20,1);
tspan=[0 1000];
[t,x] = ode45(@(t,x)fun(t,x,K),tspan, x0);
function xprime = fun(t, x, K)
theta = 0.1;dh = 0.2;phi = 0.3;ita = 0.15;
nn=20;
i = 1; f = [];
while i< nn
f = [f; (x(i)*(1 - theta*x(i)) - (x(i)*x(i+1))/(1 + x(i)));((phi*x(i)*x(i+1))/(1 + x(i)) - ita*x(i+1))];
i = i + 1;
end
xprime = f+dh*K*x;
end
%%
.
Rahim
on 20 Aug 2023
Accepted Answer
More Answers (0)
Categories
Find more on State Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
