How to solve a function with some inputs being matricies?

2 visualizzazioni (ultimi 30 giorni)
I have a function to solve that has 6 inputs(L,l,h,L2,x,h2). 3 of the inputs are constants(L,l,h). 2 of them are 1x250 matricies(L2 and h2). The final input is the variable I need to solve(x). I want the function to cycle through all the indvidual L2 and h2 values and thus produce a 1x250 matrix for x. Is there a way to do this? I have attached my code and attached my disp_function4 too.
L = 2e-3; %parameters for the non-random case
l = 5e-3;
h = 0.5e-3; %comment out the parameter your'e trying to find
n = 250; % number of points on the voronoi diagram
L2x = zeros(1,n); % array to store the L2 x values
L2y = zeros(1,n); % array to store the L2 y values
for i=1:n
q = randi([-50 50],1,1); %Parameters for the random case
L2x(i) = q.*1e-3 ; %limit that L2 must be greater than 0.5mm is obeyed
w = randi([-50 50],1,1);
L2y(i) = w.*1e-3;
P = [L2x(:) L2y(:)]; %extracting the x and y values and placing it into one matrix
L2 = sqrt(L2x.^2+L2y.^2);
end
h2 = zeros(1,n)
for i=1:n
r = (1).*rand(1,1); %100% random higher than 0.5e-3 as this the limit of tolerance of the printer
rnew = round(r,2).*1e-3; %rounds the random value to 2 decimal places
h2(i) = abs(h+rnew)
end
z = zeros(1,n);
for i=1:n
fun = @(x)disp_fun4(L,l,h,L2,x,h2) ; %change the value next to the @ with the one youre trying to find
x0 = 0; % change the '*'0 value to what youre trying to find
z(i) = fsolve(fun,x0) %where z is equal to l2 for the randomised case
end
  11 Commenti
rman
rman il 4 Lug 2022
This was just changing the line:
z(i) = fsolve(fun,x0(i)) %just changed the variable from z to x.
Torsten
Torsten il 4 Lug 2022
rng('default')
n = 10;
L = 2e-3 ; %parameters for the non-random case
l = 5e-3;
h = 0.5e-3; %comment out the parameter your'e trying to find
% number of points on the voronoi diagram
L2x = zeros(1,n); % array to store the L2 x values
L2y = zeros(1,n); % array to store the L2 y values
h2 = zeros(1,n);
z = zeros(1,n);
x0= zeros(1,n);
for i=1:n
q = randi([-50 50],1,1); %Parameters for the random case
L2x(i) = q.*1e-3 ;
w = randi([-50 50],1,1);
L2y(i) = w.*1e-3;
%P = [L2x(:) L2y(:)]; %extracting the x and y values and placing it into one matrix
%L2xx = L2x.*L2x;
%L2yy = L2y.*L2y;
%L2 = sqrt(L2xx+L2yy); %limit that L2 must be greater than 0.5mm is obeyed
L2(i) = sqrt(L2x(i)^2+L2y(i)^2);
r = rand(1,1); %100% random higher than 0.5e-3 as this the limit of tolerance of the printer
h2(i) = r*1e-3;%round(r,2).*1e-3; %rounds the random value to 2 decimal places
fun = @(x)disp_fun4(L,l,h,L2(i),x,h2(i)) ; %change the value next to the @ with the one youre trying to find
x0(i) = 0; % change the '*'0 value to what youre trying to find
options = optimset('TolFun',1e-10,'TolX',1e-10);
z(i) = fsolve(fun,x0(i),options); %where z is equal to l2 for the randomised case
fun(z(i))
end
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -7.8702e-12
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -4.5714e-10
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -3.9685e-11
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -6.9213e-09
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -9.0908e-10
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -2.4675e-08
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -2.0486e-07
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -2.1849e-10
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -4.7795e-08
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -1.7547e-07
z
z = 1×10
2.4949 2.5037 0.1728 0.9929 0.9196 0.4912 1.5144 1.0522 0.5445 1.9247
function dfun = disp_fun4(L,l,h,L2,x,h2)
omega= 2*pi*500; %parameter that doesnt change regardless of the function
rho=2700; %parameter that doesnt change regardless of the function
lambda=58e9; %parameter that doesnt change regardless of the function
mu=26e9; %parameter that doesnt change regardless of the function
rho_r = rho; %parameter that doesnt change regardless of the function
lambda_r = lambda; %parameter that doesnt change regardless of the function
mu_r = mu; %parameter that doesnt change regardless of the function
kl=omega.*sqrt(rho./(lambda+2*mu)); %parameter that doesnt change regardless of the function
kt=omega.*sqrt(rho./mu); %parameter that doesnt change regardless of the function
k = omega./2900;
E_r=mu_r.*(3.*lambda_r+2.*mu_r)/(lambda_r+mu_r);
V1=0.25*pi*h^2.*omega.*sqrt(E_r.*rho_r)*tan(l.*omega.*sqrt(rho_r./E_r));
V2=0.25*pi*h2^2.*omega.*sqrt(E_r.*rho_r)*tan(x.*omega.*sqrt(rho_r./E_r));
E=k/kt;
E2=E.*E;
r=kl./kt;
r2=r.*r;
dfun1=4*E2 .* sqrt(E2-r2).*sqrt(E2-1)-(2.*E2-1).^2-...
sqrt(E2 - r2) .* ( V1 ./ (omega.*L.^2.*sqrt(rho.*mu)));
dfun2=4*E2 .* sqrt(E2-r2).*sqrt(E2-1)-(2.*E2-1).^2-...
sqrt(E2 - r2) .* ( V2 ./ (omega.*L2.^2.*sqrt(rho.*mu)));
dfun= abs(dfun1)-abs(dfun2);
end

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Voronoi Diagram in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by