find solution of two variable wave equation and plot

3 次查看(过去 30 天)
I need a code to find the solution o a wave equation f(t)=Acos(wt-φ) when
I want to know what are the amplitude and frequency for a wave to have a rise of 1m in 100 years(876000h), but I have an additional condition. I want this to seem like a linear increase. Please take a look at the attached image for a better understanding. the cosine needs to have zero other condition is f(0)=0
I tried to use “syms” and “solve” to find the answers, but I was only getting w=0 as a solution, which doesn't make sense to me. I tried to do it, but I couldn't succeed
I would also like to see the plot of the resulting equation.
I hope this is clear, but if you have any other questions, please let me know.
Thanks in advance, I really appreciate it.
  2 个评论
Contoso Donoso
Contoso Donoso 2023-7-28
I realized my little code didn't have enough conditions to get the results I wanted, I asked ChatGPT help and came up with this code instead ( I changed hours to seconds):
% Given data points
t_data = [0, 31563000];
f_data = [0, 1];
% Define the function to fit
fun = @(A, w, t) A * cos(t * w - 90) + 0.5;
% Error function to minimize
error_func = @(x) sum((fun(x(1), x(2), t_data) - f_data).^2);
% Constraints
lb = [0.5, 0]; % Lower bounds for A and w
ub = [inf, pi/64]; % Upper bounds for A and w
% Initial guess for A and w
x0 = [1, 1e-7];
% Optimization using fmincon with 'sqp' algorithm
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
x_opt = fmincon(@(x) error_func(x), x0, [], [], [], [], lb, ub, [], options);
% Extract the optimized values for A and w
A_opt = x_opt(1);
w_opt = x_opt(2);
% Print the optimized values
disp(['Optimized A: ', num2str(A_opt)]);
disp(['Optimized w: ', num2str(w_opt)]);
% Plot the fitted curve
t_plot = linspace(0, 31563000, 1000);
f_plot = fun(A_opt, w_opt, t_plot);
plot(t_plot, f_plot);
hold on;
scatter(t_data, f_data, 'r', 'filled');
hold off;
xlabel('t');
ylabel('f(t)');
title('Fitted Harmonic Curve');
grid on;
It is not perfect because it doesn't start at zero as a wish (even though it is set as a condition), but I think it is a pretty good solution, and I think I can work with it.
I'll leave this solution as a reference for others or anyone who wishes to add any comments.
Thanks :)

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by