Repeating a function n times with different values from a vector

6 vues (au cours des 30 derniers jours)
Antonio Spiere
Antonio Spiere le 1 Mai 2019
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

Réponse acceptée

Jos (10584)
Jos (10584) le 1 Mai 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
  1 commentaire
Antonio Spiere
Antonio Spiere le 1 Mai 2019
Thank you for your awesome answer! It's working like a charm

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur App Building dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by