solving non linear equation

1 次查看(过去 30 天)
RAFFAELE CANTERI
RAFFAELE CANTERI 2021-1-20
回答: Ayush 2024-9-1
for i = 1:10
vv = vectorize('(tpiu)*((S/(2*xpiu))+((S/(4*xpiu))*sqrt(1+16*((S/xpiu)^2)))+((1/16)*log((4*S/xpiu)+sqrt(1+16*(S/xpiu)^2))))-t');
myfun = @(S,t) (tpiu).*((S./(2.*xpiu))+((S./(4.*xpiu)).*sqrt(1+16.*((S./xpiu).^2)))+((1./16).*log((4.*S./xpiu)+sqrt(1+16.*(S./xpiu).^2))))-t;
t = linspace(0, tinj, 20);
for k = 1:numel(t)
S(k) = fzero(@(S) myfun(S,t(k)),0.03);
end
end
I have this code where tinj is a vector with (i) elements and i'd like to exit with a matrix (i, k), but as I wrote the code I only get a vector relative to the last tinj.

回答(1 个)

Ayush
Ayush 2024-9-1
Hi RAFFAELE
To obtain a matrix (i, k) instead of a vector, you can preallocate the matrix S before the loop and assign the values inside the loop. Since you have not provided your complete code, you may refer to the below snippet and make the changes accordingly in your code.
S = zeros(numel(tinj), numel(t)); % Preallocate matrix S with dimensions (numel(tinj), numel(t))
for i = 1:numel(tinj)
t = linspace(0, tinj(i), 20);
for k = 1:numel(t)
S(i, k) = fzero(@(S) myfun(S,t(k)),0.03); % Find the root of myfun(S,t(k)) and store it in S(i, k)
end
end
This will give you a matrix S with dimensions (i, k), where i corresponds to the elements of tinj and k corresponds to the elements of t.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by