Repeating a function n times with different values from a vector

7 次查看(过去 30 天)
I have quite a big problem and I hope that anyone could help me.
I'm using Newton Pharson to iterate two equations with a vector x as input. Now I want to split the vector in different sections and calculate for every single section the equation and save the solutions in another new vector
Example what I mean:
x = [0.093,0.23,0.6,0.95,0.54,1]
Now I would calculate the equation for the value pairs of 0.093, 0.23 -> solution is saved to a matrix y = [2,3]
then for the next two value pairs of x (0.6 & 0.95) and save the two solutions to the matrix y = [2,3;1,2]
Does anyone have a solution to this problem?
clear all;
clc;
%% inputs:
x = [0.093,0.23,0.6,0.95,0.54,1];
% residuals
fun = @(b) ..... %enter the equation
%% solve
x0 = [3]; %initial guess
options = optimset('TolX',1e-12); % set TolX
for i =....
[b, resnorm, f, exitflag, output, jacob] = newtonraphson(fun, x0, options);
c = b +1;
end

采纳的回答

Jos (10584)
Jos (10584) 2019-5-1
If you organize the input differently, this is not so difficult
x = [1 2 ; 3 4 ; 5 6] ; % organized into rows
N = size(x,1) ; loop over rows
y = zeros(N,2) ; % pre-allocate the output
for k = 1:N
% you can do this in a single step, here I split it up in 3 for clarity
tempx = x(k,:) ;
tempy = myfunction(tempx) ;
y(k,:) = tempy ; % tempy is expected to be a 1-by-2 vector!
end

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by