How to solve a function with some inputs being matricies?

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 commentaires

Combine all the loops in one, cause they all have the same variable. Your code will take much more time to run, running all for loops individually.
Either define your whole code based on matrix operations or element operation. You have done both here and there.
Also, your condition is not satisfied here.
q = randi([-50 50],1,1) %Parameters for the random case
q = -40
L2x = q*1e-3 %limit that L2 must be greater than 0.5mm is obeyed
L2x = -0.0400
L2x is negative. Same goes for L2y.
Thank you for your help, I have adjusted the code for the condition. Are you aware of how to solve the function using fsolve in afor loop with array inputs?
Dyuman Joshi
Dyuman Joshi le 4 Juil 2022
Modifié(e) : Dyuman Joshi le 4 Juil 2022
Show the (adjusted) code.
Essentially I want the 1 x n matrix that is created in the for loops for L2 and h2 to be returned and used in the solving of the function for x. I want a 1 x n matrix of z's.
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
r = rand(1,1); %100% random higher than 0.5e-3 as this the limit of tolerance of the printer
h2(i) = round(r,2).*1e-3; %rounds the random value to 2 decimal places
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
Torsten
Torsten le 4 Juil 2022
Modifié(e) : Torsten le 4 Juil 2022
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) = 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
z(i) = fsolve(fun,x0) %where z is equal to l2 for the randomised case
end
Every time i run this it says:
Unable to perform assignment because the left and right sides have a
different number of elements.
Error in untitled168 (line 28)
z(i) = fsolve(fun,x0) %where z is equal to l2 for the randomised case
z(i) = fsolve(fun,x0(i))
instead of
z(i) = fsolve(fun,x0)
I have made this adjustment but now z is returned as 1 x n matrix full of zeros...
n = 10; % number of points on the voronoi diagram
L = 2e-3 ; %parameters for the non-random case
l = 5e-3;
h = 0.5e-3;
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);
x = 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
L2(i) = sqrt(L2x(i)^2+L2y(i)^2); %limit that L2 must be greater than 0.5mm is obeyed
r = rand(1,1); %100% random higher than 0.5e-3 as this the limit of tolerance of the printer
h2(i) = 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
x(i) = fsolve(fun,x0(i)) ; %where z is equal to l2 for the randomised case
end
Where did I write
x(i) = fsolve(fun,x0(i)) ;
?
This was just changing the line:
z(i) = fsolve(fun,x0(i)) %just changed the variable from z to x.
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

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Produits

Version

R2021b

Question posée :

le 1 Juil 2022

Commenté :

le 4 Juil 2022

Community Treasure Hunt

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

Start Hunting!

Translated by