I Need Help With The Coding/Syntax Aspects Of A Modeling Project In Matlab

Posted Under: Matlab

Ask A Question
DESCRIPTION
Posted
Modified
Viewed 14

I attached the pdf for problem. I know the analytic solution to the spring model that I want to use for Part 1 is the following (based on complex solution):

 

I need help fixing my code to get reasonable estimates for the parameters C and K. I also need my model to more closely fit the provided data (first column=t_values, second column= y_values):

 0.0000000000000000e+00   1.9683560412640260e+00
   5.0000000000000003e-02   2.5401155097640582e+00
   1.0000000000000001e-01   2.6992180590842731e+00
   1.5000000000000002e-01   2.8497851392735480e+00
   2.0000000000000001e-01   2.9164132308893325e+00
   2.5000000000000000e-01   2.9887945295672442e+00
   3.0000000000000004e-01   2.7256130820014515e+00
   3.5000000000000003e-01   2.7249726912711489e+00
   4.0000000000000002e-01   2.2949310330607728e+00
   4.5000000000000001e-01   1.8889106881520503e+00
   5.0000000000000000e-01   1.4211332975180251e+00
   5.5000000000000004e-01   1.0314747810717186e+00
   6.0000000000000009e-01   6.9802593483661057e-01
   6.5000000000000002e-01  -1.0499037138706752e-01
   7.0000000000000007e-01  -1.3350001160592762e-01
   7.5000000000000000e-01  -6.6655901750093582e-01
   8.0000000000000004e-01  -9.3331680496275671e-01
   8.5000000000000009e-01  -1.0891316244775093e+00
   9.0000000000000002e-01  -1.3061677029583867e+00
   9.5000000000000007e-01  -1.6569875140760810e+00
   1.0000000000000000e+00  -1.7015269834301809e+00
   1.0500000000000000e+00  -1.4784542252254362e+00
   1.1000000000000001e+00  -1.5725351706867112e+00
   1.1500000000000001e+00  -1.5368561979064321e+00
   1.2000000000000002e+00  -1.2504504246470898e+00
   1.2500000000000000e+00  -1.0686914119178776e+00
   1.3000000000000000e+00  -9.3959760347356613e-01
   1.3500000000000001e+00  -7.1115792012367318e-01
   1.4000000000000001e+00  -3.3668922189904321e-01
   1.4500000000000002e+00  -3.4415227297928841e-01
   1.5000000000000000e+00   2.0533484505197874e-01
   1.5500000000000000e+00   3.2949218503530159e-01
   1.6000000000000001e+00   3.9854339162142699e-01
   1.6500000000000001e+00   5.6381184532649542e-01
   1.7000000000000002e+00   5.7809891665361235e-01
   1.7500000000000000e+00   6.3776769145106749e-01
   1.8000000000000000e+00   8.2696247360643915e-01
   1.8500000000000001e+00   8.9066820724724505e-01
   1.9000000000000001e+00   9.0851045232450389e-01
   1.9500000000000002e+00   7.1236326643184844e-01
   2.0000000000000000e+00   7.1853749350626717e-01
   2.0500000000000003e+00   7.6933765157813716e-01
   2.1000000000000001e+00   6.9792006666597084e-01
   2.1499999999999999e+00   3.0822116404974675e-01
   2.2000000000000002e+00   4.6536738627047114e-01
   2.2500000000000000e+00   9.7705172582468636e-02
   2.3000000000000003e+00  -7.0671562735974025e-02
   2.3500000000000001e+00  -4.0590067642366286e-02
   2.4000000000000004e+00  -1.9604163510434142e-01
   2.4500000000000002e+00  -1.4565982177718087e-01
   2.5000000000000000e+00  -2.5965272405273288e-01
   2.5499999999999998e+00  -5.3547986299248462e-01
   2.5999999999999996e+00  -7.1755230764097366e-01
   2.6499999999999999e+00  -3.5325261214141146e-01
   2.6999999999999997e+00  -5.0995255027860431e-01
   2.7500000000000000e+00  -2.8861252935968829e-01
   2.7999999999999998e+00  -1.8503628812852196e-01
   2.8500000000000001e+00  -4.1043081919704305e-01
   2.8999999999999999e+00  -1.5080652546909365e-01
   2.9499999999999997e+00  -1.8319894027169192e-01
   3.0000000000000000e+00  -2.0173566406611218e-01
   3.0499999999999998e+00   8.5074312841789693e-02
   3.0999999999999996e+00  -7.5784231060834317e-02
   3.1499999999999999e+00   1.6183236291406872e-01
   3.2000000000000002e+00   2.4835002043505017e-01
   3.2500000000000000e+00  -9.4558614989725773e-02
   3.2999999999999998e+00   4.6828779907812917e-01
   3.3499999999999996e+00   3.2702358751667288e-01
   3.3999999999999999e+00   3.1194295814812040e-01
   3.4500000000000002e+00   1.2959364624636296e-01
   3.5000000000000000e+00   1.8288236474296396e-01
   3.5499999999999998e+00   5.8141442731960286e-02
   3.5999999999999996e+00   5.4346333281654502e-02
   3.6499999999999999e+00   7.2951424612087534e-02
   3.7000000000000002e+00   2.0355709564223870e-01
   3.7500000000000000e+00   3.0671096299140477e-01
   3.7999999999999998e+00   2.2453539253023000e-01
   3.8499999999999996e+00   6.3703089839303728e-04
   3.8999999999999999e+00  -2.0673008997557379e-01
   3.9500000000000002e+00   1.7588448689622557e-02
   4.0000000000000000e+00  -1.1079700111572058e-01
   4.0499999999999998e+00  -2.4338937968371596e-01
   4.0999999999999996e+00  -2.8971565241816981e-02
   4.1500000000000004e+00  -2.9901105997684536e-02
   4.2000000000000002e+00  -1.1400395081135342e-02
   4.2500000000000000e+00  -1.3500489847075375e-01
   4.2999999999999998e+00  -1.5229388200032096e-01
   4.3499999999999996e+00  -1.2740917565903187e-01
   4.4000000000000004e+00  -1.1772676881791497e-01
   4.4500000000000002e+00  -6.7072080061519546e-02
   4.5000000000000000e+00   9.8881921219815011e-02
   4.5499999999999998e+00  -5.1185315647399983e-02
   4.5999999999999996e+00   1.9822950458319201e-02
   4.6500000000000004e+00   1.8563342521856621e-01
   4.7000000000000002e+00  -5.2249182594638538e-02
   4.7500000000000000e+00   7.5136038047436751e-02
   4.7999999999999998e+00   4.3489466293947010e-02
   4.8499999999999996e+00  -6.3868095249882600e-02
   4.9000000000000004e+00  -2.2994379041232910e-01
   4.9500000000000002e+00  -2.0522390027295268e-01
   5.0000000000000000e+00   1.4140869981408358e-02

 

Here is the MatLab code I've tried so far:

 

%MA Project 2- Problem1
% Provided data
load Data.txt
t_values=Data(:,1)
y_values=Data(:,2)

% Define a function to calculate residuals
residuals = @(params) (exp(-params(1)/2 .* t_values)) .* (2 * cos(sqrt(4 * params(2) - params(1)^2)/2 .* t_values) + (20 + 2 * params(1)) / sqrt(4 * params(2) - params(1)^2) .* sin(sqrt(4 * params(2) - params(1)^2)/2 .* t_values) - y_values);


% Create an anonymous function for optimization
objective_function = @(params) sum(residuals(params).^2);

% Initial guess for parameters C and K
initial_guess = [1, 5];  % You can adjust these initial values as needed

% Use fminsearch to minimize the sum of squared residuals
optimal_params = fminsearch(objective_function, initial_guess);

% Extract the estimated values of C and K
C_estimated = optimal_params(1);
K_estimated = optimal_params(2);

C_estimated
K_estimated

objective_value = objective_function([C_estimated, K_estimated])

C=C_estimated;
K=K_estimated;
f = exp(-C/2 * t_values) .* (2 .* cos(sqrt(4 * K - C^2)/2 .* t_values)) + (20 + (2 * C)) ./ sqrt(4 * K - C^2) .* sin(sqrt(4 * K - C^2)/2 .* t_values);

%plotting model vs data
figure;
plot(t_values,f, 'b-', 'LineWidth', 2); 
hold on;
plot(t_values,y_values, 'ro', 'MarkerSize', 3, 'MarkerFaceColor', 'r');
xlabel('Time (t)');
ylabel('Displacement (y)');
title('Model vs Data');
legend('Model (f)', 'Data (y)', 'Location', 'Best');
hold off;

%Plotting the residuals
residual_values = f-y_values;
figure;
plot(t_values, residual_values, 'x', 'MarkerSize', 5);
hold on
line([min(t_values), max(t_values)], [0, 0], 'Color', 'r', 'LineStyle', '-')
xlabel('Time (t)');
ylabel('Residuals');
title('Residuals vs Time');
%Errors do not appear to be iid (???)


%Estimate for the variance
n=101;
p=2;
Var_Est=1/(n-p)*residual_values'*residual_values

syms C K t

% Define the original function f
f = exp(-C/2 * t) * (2 * cos(sqrt(4 * K - C^2)/2 * t)) + (20 + (2 * C)) / sqrt(4 * K - C^2) * sin(sqrt(4 * K - C^2)/2 * t);

% Compute the partial derivatives
df_dC = diff(f, C)  % Partial derivative of f with respect to C
df_dK = diff(f, K)  % Partial derivative of f with respect to K

 

Current figure looks like this:

 

I would like to get a result that makes sense, changing as little of my original code as possible for parts (i) through (iii). Then I need help writing the code for part (iv).

 

Attachments
Explanations and Answers 0

No answers posted

Post your Answer - free or at a fee

Login to your tutor account to post an answer

Posting a free answer earns you +20 points.

Login

NB: Post a homework question for free and get answers - free or paid homework help.

Get answers to: I Need Help With The Coding/Syntax Aspects Of A Modeling Project In Matlab or similar questions only at Tutlance.

Related Questions