Using lsqcurvefit during conditions

Hi
I have a data which I must to fit it with :
I have an Excel file:
My code is :
clc
clear all
Data=load('1.csv');
t=Data(:,1);
x_data=Data(:,2);
y=Data(:,3);
fun=@(x,x_data)x*sqrt(((x_data/x).^2)-1)*sign(x_data);
x0=0.72*10^-6; %initial
A = lsqcurvefit(fun,x0,x_data,y);
But my answer is imaginary every time...
What is wrong?!

3 commentaires

Walter Roberson
Walter Roberson le 17 Sep 2020
Your fun() is not taking into account the abs(x_data/x) > 1 part
But since x_data is a vector of values, it is not clear which x_data to be using for abs(x_data/x) > 1 ?
The value I find is about 5E-9 with any value below that not making any notable change, until you get down to sqrt(realmin) at which point you get numeric problems.
Pouyan Missaghian
Pouyan Missaghian le 17 Sep 2020
Can I see your code ? I dont know how to tell Matlab that there is a condition abs(x_data/x) > 1 ....
Data=load('1.csv');
x_data=Data(:,2);
y=Data(:,3);
fun=@(x,x_data)x.*sqrt(((x_data./x).^2)-1).*sign(x_data).*(abs(x_data./x) > 1);
f_vec = @(x) sum((fun(x,x_data)-y).^2);
x0=0.72*10^-6; %initial
fminunc(f_vec, x0)
ans =
5.423876953125e-07
but then I tested by hand to see if a better result was available, and found that you stopped getting changes about f_vec(5e-9)

Connectez-vous pour commenter.

Réponses (1)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by