Azzera filtri
Azzera filtri

I want to use fzero to find the root of a transcendental function r. I have problem on line 33-37 and 52-63

3 visualizzazioni (ultimi 30 giorni)
%% initialization
clear
clc
close all
%% Model parameters
global k1 eta1 alpha3 gamma1 d N Phi xi h C q qstar Theta OddAsymptotes m ...
qq r1 r2 alpha aa b Efun F B Ainv r v theta_sol Vfun v_sol p ModifiedOddAsym ...
qstarTemporary theta_init z
k1 = 6*10^(-12); % elastic constant
eta1 = 0.0240; % viscosity
xi = 0.585; % activity parameter
alpha3 = -0.001104; % viscosity
gamma1 = 0.1093; % viscosity
Theta = 0.0001;
theta_init = Theta.*sin(pi*z/d);
d = 0.0002;
N = 20;
% Simplified parameters
alpha=1-alpha3^2/gamma1/eta1;
C = (alpha3*xi*alpha/eta1)./(4*k1*q.^2/d^2-alpha3*xi/eta1);
% Plotting r(q)
q=0:0.01:(5*pi);
r=q-(1-alpha).*tan(q)+ C.*tan(q);
Error using .*
Arrays have incompatible sizes for this operation.
figure
plot(q,r)
axis([0 5*pi -20 20])
% Assign g for q and plot r, and compute the special asymptotes
syms C
for q=0:N
qstar(i) = solve(1/C == 0);
end
% Compute Odd asymptote
for j=1:N
OddAsymptotes(j)= 1/2*(2*j + 1)*pi;
end
ModifiedOddAsym = [abs(OddAsymptotes) abs(qstar)];
qstarTemporary = min(ModifiedOddAsym);
% Compute the rest of the Roots
for j=1:N
OddAsymptotes(j)= 1/2.*(2*j + 1)*pi;
end
% Compute all symptotes
AllAsymptotes = [sort(OddAsymptotes) sort(qstar)];
q0 =0;
for q = 0.001:AllAsymptotes(1);
r1 = fzero(r, q0)
end
for i=1:N
for q =AllAsymptotes(i):AllAsymptotes(i+1);
r2 = fzero(r, q0)
end
end
% Compute all roots
gg = [r1 r2];
% Remove the repeated roots if any & redefine n
qq = unique(gg);
m = length(qq);
% Compute the time constants tan_n
for i=1:m
p(i) = (gamma1*alpha)./(4*k1*qq(i)^2/d^2-alpha3*xi/eta1);
end
% Compute integral coefficients for off-diagonal elements θ_n matrix
for i=1:m
for j= i+1:m
for z=0:d
b(i, j) = int(Efun(i).*Efun(j));
b(j, i) = b(i, j);
aa(i, j) = b(i, j);
aa(j, i)= b(j, i);
end
end
end
% Calculate integral coefficients for diagonal elements in theta_n matrix
for i=1:m
for z=0:d
aa(i, i)= int(Efun(i).^2);
end
end
% Calculate integrals for RHS vector
for i=1:m
for z=0:d
F(i) = int(theta_init.*Efun(i));
end
end
% Create matrix A and RHS vector B
for i=1:m
for j= 1:m
aa(i, j)
end
end
for i=1:m
B = F(i)
end
% Calculate inverse of A and solve A*x=B
Ainv = inv(A);
r = B*Ainv;
% Define Theta(z,t)
for i=1:m
theta_sol(i) = sum(r(i).*Efun(i).*exp(-t/p(i)));
end
% Compute v_n for n times constant
for i=1:m
v(i) = (-2*k1*alpha3*qq(i))*(1/(d^2*eta1*alpha*gamma1))+ alpha3^2*xi/(2*(eta1)^2*qq(i)*alpha*gamma1)+xi/(2*eta1*qq(i));
end
% Compute v(z,t) from initial conditions
for i=1:m
Vfun(i) = d*sin(qq(i)-2*qq(i)*z/d)+(2*z-d)*sin(qq(i));
end
% Define v(z,t)
for i=1:m
v_sol = sum(v(i).*r(i)*Vfun(i).*exp(-t/p(i)));
end
  17 Commenti
Steven Lord
Steven Lord il 25 Ago 2022
You've posted a lot of code. But let's take a step back for a second. Can you explain in words not code the problem you're trying to solve? What do you know and what are you trying to find or compute? Knowing the context may help us offer better guidance in how to adjust the code.
University Glasgow
University Glasgow il 26 Ago 2022
Modificato: Walter Roberson il 26 Ago 2022
First of all, I have a problem with computing the root of the function r. It seems that maple do skip some roots of the function r that's why I have change to matlab. I want to compute the roots of r for q=(2*i-2)*Pi/2..(2*i+1)*Pi/2) and assign the results to gg, if there is any repeated, i used unique() to remove the repeated roots and finally assign the result to qq and use qq to compute p(i), theta_sol and v_sol. I don't know how to find roots with matlab. I tried vpasolve() but i need syms which didn't work in my case.
Finally, I want to plot theta_sol and v_sol for various times at z=0:d.
%% initialization
clear
clc
close all
%% Model parameters
global k1 eta1 alpha3 gamma1 N xi C q qstar Theta OddAsymptotes m ...
qq alpha aa b Efun F B r p ModifiedOddAsym qstarTemporary ...
AllAsymptotes gg Efun2 Efun1 v theta_sol Vfun Efun3 v_sol d z
k1 = 6*10^(-12); % elastic constant
eta1 = 0.0240; % viscosity
xi = 0.01; % activity parameter
alpha3 = -0.001104; % viscosity
gamma1 = 0.1093; % viscosity
Theta = 0.0001;
d = 0.0002;
N = 20;
% Simplified parameters
alpha=1-alpha3^2/gamma1/eta1;
C = (alpha3*xi*alpha/eta1)./(4*k1*q.^2/d^2-alpha3*xi/eta1);
%% Plotting r(q)
t = [0:0.1:10];
nt=length(t);
q=0:0.01:(5*pi);
r=q-(1-alpha).*tan(q)+ C.*tan(q);
figure
plot(q,r)
axis([0 5*pi -20 20])
% Remove the repeated roots if any & redefine n
qq = unique(gg);
m = length(qq);
% Compute the time constants tan_n
for i=1:m
p(i) = (gamma1*alpha)./(4*k1*qq(i)^2/d^2-alpha3*xi/eta1);
end
% Compute theta_n from initial conditions
Efun = @(z, i, d)cos(qq(i)-2*qq(i).*z/d)-cos(qq(i));
Efun2 = @(z, j, d)cos(qq(i)-2*qq(j).*z/d)-cos(qq(j));
% Compute integral coefficients for off-diagonal elements θ_n matrix
b = zeros(m, m);
for i=1:m
for j= i+1:m
b(i, j) = integral(@(z)(Efun(z, i, d).*Efun2(z, j, d)), 0, d);
b(j, i) = b(i, j);
aa(i, j) = b(i, j);
aa(j, i) = b(j, i);
end
end
% Calculate integral coefficients for diagonal elements in theta_n matrix
for i=1:m
aa(i, i)= integral((@(z)Efun(z, i, d).^2), 0, d);
end
% Calculate integrals for RHS vector
Efun1 = @(z, i, d, Theta) Theta.*sin(pi*z/d).*cos(qq(i)-2*qq(i).*z/d)-cos(qq(i));
for i=1:m
F(i) = integral(@(z)Efun1(z, i, d, Theta), 0, d);
end
% Create matrix A and RHS vector BN_mat = N-2;
for i=1:m
for j= 1:m
A(i, j) = aa(i, j);
B(i) = F(i);
end
end
% Calculate inverse of A and solve A*x=B
Ainv = inv(A);
b = Ainv\B';
% Compute theta(z,t) from initial conditionsDefine Theta(z,t)
for i=1:m
z = 0:d;
Efun3(i) = cos(qq(i)-2*qq(i).*z/d)-cos(qq(i));
theta_sol(i) = sum(b(i).*Efun3(i).*exp(-t/p(i)));
end
% Compute v_n for n times constant
for i=1:m
v(i) = (-2*k1*alpha3*qq(i))*(1/(d^2*eta1*alpha*gamma1))+ alpha3^2*xi/(2*(eta1)^2*qq(i)*alpha*gamma1)+xi/(2*eta1*qq(i));
end
% Compute v(z,t) from initial conditions and define v(z,t)
for i=1:m
Vfun(i) = d*sin(qq(i)-2*qq(i)*z/d)+(2*z-d)*sin(qq(i));
v_sol = sum(v(i).*r(i)*Vfun(i).*exp(-t/p(i)));
end

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Function Handles in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by