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
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
L2x = q*1e-3 %limit that L2 must be greater than 0.5mm is obeyed
L2x is negative. Same goes for L2y.
rman
2022년 7월 4일
Dyuman Joshi
2022년 7월 4일
편집: Dyuman Joshi
2022년 7월 4일
Show the (adjusted) code.
rman
2022년 7월 4일
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
rman
2022년 7월 4일
Torsten
2022년 7월 4일
z(i) = fsolve(fun,x0(i))
instead of
z(i) = fsolve(fun,x0)
rman
2022년 7월 4일
Torsten
2022년 7월 4일
Where did I write
x(i) = fsolve(fun,x0(i)) ;
?
rman
2022년 7월 4일
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
z
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
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!