Repeating a function n times with different values from a vector

7 views (last 30 days)
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

Accepted Answer

Jos (10584)
Jos (10584) on 1 May 2019
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

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!