Repeating a function n times with different values from a vector

4 ビュー (過去 30 日間)
Antonio Spiere
Antonio Spiere 2019 年 5 月 1 日
コメント済み: Antonio Spiere 2019 年 5 月 1 日
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
  1 件のコメント
Antonio Spiere
Antonio Spiere 2019 年 5 月 1 日
Thank you for your awesome answer! It's working like a charm

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by