Clear Filters
Clear Filters

Same code in different system giving different output

44 views (last 30 days)
I’ve written a code that collects data from an Excel file, then enters a while loop for iteration using fminsearch along with two custom functions I’ve developed. The loop exits based on a specific breaking condition. All my files are stored in a OneDrive folder, which I can access both from my desktop and my laptop.
However, I’m experiencing an issue: When I run the code on my desktop, it completes 114 iterations, while on my laptop, it runs for 314 iterations. Additionally, the output values differ between the two systems. Since the code file is located in OneDrive, I’m using the exact same code (without copying or making separate files). The only difference in the code is how it reads the Excel file—I modify the file path depending on whether I’m working on my desktop or laptop, and I handle this through commented-out lines. I am using the same version of matlab in both system.
Could anyone kindly provide insight into why this is happening? I would also appreciate any suggestions on how I can ensure consistent results across both systems.
  3 Comments
H M Dilshad Alam
H M Dilshad Alam on 19 Sep 2024 at 19:54
clear all
%dataset for laptop
data = xlsread("locationfromlaptop\1.xlsx");
%dataset for Lab pc
%data = xlsread("locationfromdesktop\1.xlsx");
x = data(:, 1:5);
y = data(:, 6);
beta = [719.9786; -80.0245; -80.8986; 20.72844; -0.54265; -211.659; -35.3204;
16.67953; -0.96645; 0.104964; 31.0675; 271.1223; 4.225911; -0.09841;
69.22954; -0.91565; 0.002612; -7.39912; 3.05E-05; 0.114144; 74.11468];
tolerance = 1e-5;
max_iterations = 1000;
iteration = 0;
fvalthetas = [];
fvalbetas = [];
theta0 = zeros(1, 21);
while iteration < max_iterations
iteration = iteration + 1;
options = optimset('Display', 'iter');
[theta, fvaltheta] = fminsearch(@(theta) computeSumNew(theta, beta, x, y), theta0, options);
disp(fvaltheta)
fvalthetas = [fvalthetas; fvaltheta];
[beta_new, fvalbeta] = fminsearch(@(beta) weightedSumNew(beta, theta, x, y), beta, options);
disp(fvalbeta)
fvalbetas = [fvalbetas, fvalbeta];
disp(iteration)
if max(abs(beta_new - beta)) < tolerance
break;
end
beta = beta_new;
end
disp('Converged Beta:');
disp(beta);
disp('Converged Theta:');
disp(theta);
The functions used in the codes are:
function result = computeSumNew(theta, beta, x, y)
X = [ones(size(x, 1), 1), x, x(:, 1).^2, x(:, 1).*x(:, 2), x(:, 1).*x(:, 3), x(:, 1).*x(:, 4), x(:, 1).*x(:, 5), ...
x(:, 2).^2, x(:, 2).*x(:, 3), x(:, 2).*x(:, 4), x(:, 2).*x(:, 5), ...
x(:, 3).^2, x(:, 3).*x(:, 4), x(:, 3).*x(:, 5), ...
x(:, 4).^2, x(:, 4).*x(:, 5), ...
x(:, 5).^2];
output = X * beta;
theta_term = X * theta';
sum_result = 0;
for i = 1:length(y)
squared_diff = (y(i) - output(i)).^2;
lggd = log(squared_diff);
term1 = lggd - theta_term(i);
term = term1^2;
sum_result = sum_result + term;
end
result = sum_result;
end
the other one
function result = weightedSumNew(beta, theta, x, y)
X = [ones(size(x, 1), 1), x, x(:, 1).^2, x(:, 1).*x(:, 2), x(:, 1).*x(:, 3), x(:, 1).*x(:, 4), x(:, 1).*x(:, 5), ...
x(:, 2).^2, x(:, 2).*x(:, 3), x(:, 2).*x(:, 4), x(:, 2).*x(:, 5), ...
x(:, 3).^2, x(:, 3).*x(:, 4), x(:, 3).*x(:, 5), ...
x(:, 4).^2, x(:, 4).*x(:, 5), ...
x(:, 5).^2];
output = X * beta;
sum_result = 0;
for i = 1:length(y)
theta_term = X(i, :) * theta';
wi = exp(-theta_term);
squared_diff = (y(i) - output(i)).^2;
term = wi * squared_diff;
sum_result = sum_result + term;
end
result = sum_result;
end
The data is a excel sheet with first 5 coloums showing 5 variables of x and the 6th coloum is the y value. There are total 50 data points.

Sign in to comment.

Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!