How can I put Excel data into the function?

1 次查看(过去 30 天)
function [y] = rosensc(xx)
xx = [x1 x2 x3 x4];
for ii = 1:4
xxbar(ii) = 15*xx(ii) - 5;
end
sum = 0;
for ii = 1:3
xibar = xxbar(ii);
xnextbar = xxbar(ii+1);
new = 100*(xnextbar-xibar^2)^2 + (1-xibar)^2;
sum = sum + new;
end
y = (sum - 3.827*10^5) / (3.755*10^5);
end
I have Excel file about x1 x2 x3 and x4 value.
So I want to get 20 Y values about x1 x2 x3 x4.
Please let me know. Thank you!

采纳的回答

Walter Roberson
Walter Roberson 2021-7-14
format long g
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/683938/rosen20.xlsx';
data = readmatrix(filename);
h = height(data);
Y = zeros(h,1);
for K = 1 : h
Y(K) = rosensc(data(K,:));
end
Y
Y = 20×1
104221.223285056 18421.5879316538 12369.3018011083 14678.1935484914 6757.98685897914 770.00639995527 76930.949171674 107554.180109397 160124.763289724 13632.6698832972
function [y] = rosensc(xx)
for ii = 1:4
xxbar(ii) = 15*xx(ii) - 5;
end
sum = 0;
for ii = 1:3
xibar = xxbar(ii);
xnextbar = xxbar(ii+1);
new = 100*(xnextbar-xibar^2)^2 + (1-xibar)^2;
sum = sum + new;
end
y = (sum - 3.827*10^5) / (3.755*10^5);
end

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by